// register your Gadget's namespace
registerNamespace("Gongdosoft.Gadget.Test");
// define the constructor for your Gadget (this must match the name in the manifest xml)
Gongdosoft.Gadget.Test.Test04 = function(p_elSource, p_args, p_namespace) {
// always call initializeBase before anything else!
Gongdosoft.Gadget.Test.Test04.initializeBase(this, arguments);
// private member variables for gadget
var m_this = this;
var m_el = p_elSource;
var m_module = p_args.module;
// user private member variables
var m_divBG = null; // <div> : 백그라운드
m_imgGhost = new Array(2), // <img> : 메인 캐릭터
m_sBaseUrl = "http://www.3m4you.co.kr/gongdo/gadgets/Test04/"; // 개짓이 위치하는 URL
m_nGhostSpeed = 3; // 캐릭터의 속도
m_nPosX = 0, // 메인 캐릭터의 X,Y 위치
m_nPosY = 0,
m_nPrevPosX = 0, // 메인 캐릭터의 이전 X,Y 위치
m_nPrevPosY = 0,
m_nInterval = 30, // 메인 프로시져의 처리 주기
m_nTimeouts = 0, // MouseMove 이후 타이머 회수 체크
m_nMaxTimeouts = 40, // 캐릭터의 생명 주기
m_tmrMain = null; // 메인 타이머
/****************************************
** initialize Method
****************************************/
// initialize is always called immediately after your object is instantiated
this.initialize = function(p_objScope)
{
// always call the base object's initialize first!
Gongdosoft.Gadget.Test.Test04.getBaseMethod(this, "initialize", "Web.Bindings.Base").call(this, p_objScope);
//TODO: perform any initialization/setup work
// Create objects
CreateObjects();
m_module.resize();
};
Gongdosoft.Gadget.Test.Test04.registerBaseMethod(this, "initialize");
/****************************************
** dispose Method
****************************************/
this.dispose = function(p_blnUnload) {
//TODO: add your dispose code here
// Destroy objects
DestroyObjects();
// null out all member variables
m_this = null;
m_el = null;
m_module = null;
// always call the base object's dispose last!
Gongdosoft.Gadget.Test.Test04.getBaseMethod(this, "dispose", "Web.Bindings.Base").call(this, p_blnUnload);
};
Gongdosoft.Gadget.Test.Test04.registerBaseMethod(this, "dispose");
/****************************************
** Other Methods
****************************************/
//==========================================================================
// C'tor, D'tor, Event Handlers
//==========================================================================
/*--------------------------------------
| CreateObjects
| 필요한 모든 객체를 생성하고 이벤트를 바인딩
---------------------------------------*/
function CreateObjects()
{
// 메인 백그라운드 <div>
var objTmp = null;
objTmp = document.createElement("div");
objTmp.id = "divBG";
objTmp.style.pixelWidth = 300; // 최소 사이즈 결정
objTmp.style.pixelHeight = 200;
objTmp.style.backgroundColor = "#FF6633";
m_divBG = m_el.appendChild(objTmp);
// 메인 캐릭터 이미지 <img>
for (i=0;i<2;i++)
{
objTmp = document.createElement("img");
objTmp.id = "imgGhost";
objTmp.src = m_sBaseUrl + "images/ghost_ani0" + (i+1) + ".gif";
objTmp.style.position = "absolute";
objTmp.style.left = 50;
objTmp.style.top = 50;
objTmp.style.visibility = "hidden";
m_imgGhost[i] = m_el.appendChild(objTmp);
}
// 이벤트 매핑
AttachEvent(m_divBG, "mousemove", MouseMoveBG);
// 타이머 시작
m_tmrMain = setTimeout(MainProcedure, m_nInterval);
objTmp = null;
} // function CreateObjects()
/*--------------------------------------
| DestroyObjects
| 모든 이벤트를 해제하고 생성한 객체를 파괴
---------------------------------------*/
function DestroyObjects()
{
DetachEvent(m_divBG, "mousemove", MouseMoveBG);
clearTimeout(m_tmrMain); // 타이머 제거
m_tmrMain = null;
m_divBG = null;
m_imgGhost = null;
} // function DestroyObjects()
/*--------------------------------------
| MouseMoveBG
| 백그라운드에서 마우스가 움직일 때 이벤트
---------------------------------------*/
function MouseMoveBG()
{
var nPosX = 0, nPosY = 0;
nPosX = event.x;
nPosY = event.y;
m_nPrevPosX = m_nPosX;
m_nPrevPosY = m_nPosY;
m_nPosX = nPosX;
m_nPosY = nPosY;
//m_divBG.innerHTML = "Cur X : " + nPosX + ", Cur Y : " + nPosY + "<BR>"
//m_divBG.innerHTML = m_divBG.innerHTML + "X : " + m_nPosX + ", Y : " + m_nPosY + " / " + m_nTimeouts;
m_nTimeouts = 0; // 타이머 타임아웃 카운트 초기화
// 캐릭터 보여줌
ShowGhost();
} // function MouseMoveBG()
//==========================================================================
// Procedures
//==========================================================================
/*--------------------------------------
| MainProcedure
| 메인 프로시저
---------------------------------------*/
function MainProcedure()
{
clearTimeout(m_tmrMain); // 타이머 제거
// MouseMove로부터 Timeout된 회수 체크하여 캐릭터를 숨길지 여부 확인
if (m_nTimeouts < m_nMaxTimeouts)
{
// 타임아웃되기 전까지 캐릭터를 마우스 포지션까지 이동
//m_divBG.innerHTML = "Timeup : " + m_nTimeouts;
var nImgX = 0, nImgY = 0;
nImgX = parseInt(m_imgGhost[0].style.left);
nImgY = parseInt(m_imgGhost[0].style.top);
if (m_nPosX < (nImgX - m_nGhostSpeed))
{
// 왼쪽 보고 있는 경우
m_imgGhost[0].style.left = (nImgX - m_nGhostSpeed);
m_imgGhost[1].style.left = (nImgX - m_nGhostSpeed);
}
elseif (m_nPosX > (nImgX + m_nGhostSpeed))
{
// 오른쪽 보고 있는 경우
m_imgGhost[0].style.left = (nImgX + m_nGhostSpeed);
m_imgGhost[1].style.left = (nImgX + m_nGhostSpeed);
}
// 어느쪽이든 Y 위치 조정
if (m_nPosY < (nImgY - m_nGhostSpeed))
{
m_imgGhost[0].style.top = (nImgY - m_nGhostSpeed);
m_imgGhost[1].style.top = (nImgY - m_nGhostSpeed);
}
elseif (m_nPosY > (nImgY + m_nGhostSpeed))
{
m_imgGhost[0].style.top = (nImgY + m_nGhostSpeed);
m_imgGhost[1].style.top = (nImgY + m_nGhostSpeed);
}
m_nTimeouts = m_nTimeouts + 1;
}
else
{
// 시간 초과되면 숨김
for(i=0;i<2;i++)
{
m_imgGhost[i].style.visibility = "hidden";
}
//m_divBG.innerHTML = "Cleanup"
}
m_tmrMain = setTimeout(MainProcedure, m_nInterval);
} // function MainProcedure()
/*--------------------------------------
| ShowGhost
| 현재 커서 위치에 따라 적당한 고스트를 보여줌
---------------------------------------*/
function ShowGhost()
{
//m_divBG.innerHTML = "Xpos : " + m_nPosX + " ImgX : " + m_imgGhost[0].style.left + " ImgY : " + m_imgGhost[0].style.top;
var nPosX = 0;
nPosX = parseInt(m_imgGhost[0].style.left);
if (m_nPosX < nPosX)
{
if (m_imgGhost[0].style.visibility != "visible")
{
m_imgGhost[0].style.visibility = "visible";
m_imgGhost[1].style.visibility = "hidden";
}
}
else
{
if (m_imgGhost[1].style.visibility != "visible")
{
m_imgGhost[0].style.visibility = "hidden";
m_imgGhost[1].style.visibility = "visible";
}
}
} // function ShowGhost()
/*--------------------------------------
| HideGhost
| 고스트를 숨김
---------------------------------------*/
function HideGhost()
{
m_imgGhost[0].style.visibility = "hidden";
m_imgGhost[1].style.visibility = "hidden";
} // function HideGhost()
//==========================================================================
// Utilities
//==========================================================================
/*--------------------------------------
| AttachEvent
| 주어진 엘리먼트에 이벤트 및 핸들러를 연결
---------------------------------------*/
function AttachEvent(el, obj, f)
{
el.attachEvent("on" + obj, f);
}
/*--------------------------------------
| DetachEvent
| 주어진 엘리먼트에 이벤트 및 핸들러를 해제
---------------------------------------*/
function DetachEvent(el, obj, f)
{
el.detachEvent("on" + obj, f);
}
};
Gongdosoft.Gadget.Test.Test04.registerClass("Gongdosoft.Gadget.Test.Test04", "Web.Bindings.Base");
Submit comment.
비밀댓글입니다
비공개// 에효 결국 별 도움은 안된 것 같네요.
개짓은 당분간 접어두고 있긴하지만 WPF공부가 어느정도 진행되고 뭔가 만들어볼만 하면 개짓도 손을 대지 않을까 해요.
의견 공유야 언제든지 환영이에요. 질문이든 뭐든 OK. :)