회원로그인
onEnterFrame의 제거 - this - parent
15년 전
onEterFrame 이벤트를 적절한 시점에서 제거.
->부드러운 움직임.. (목적지까지 도착후엔 필요가 없는 액션)
delete this.onEnterFrame;
----------------------------------------------------------------------------------------
예제1)
_root._mc.count = 0;
_root._mc.onEnterFrame = function(){
this._x = this._x + 0.2 *(500 - this._x);
//거의 목적지에 도착하면, 매 프레임마다 할 일 제거
if ( ++this.count > 30 ){
this._x = 500;
delete this.onEnterFrame;
}
};
// 속도계수가 0.2일때는 대략 30프레임정도면 목적지에 도달한다
----------------------------------------------------------------------------------------
예제2)
/* 일반적인 코딩
_root._mc.onEnterFrame = function(){
this._x = this._x + 0.2* (this.tx - this._x);
this._y = this._y + 0.2 * (this.ty - this._y);
};
_root._mc.onMouseDown = function(){
this.tx = _root._xmouse;
this.ty = _root._ymouse;
}; */
/* 마우스를 누르면
마우스 위치를 tx, ty로 저장하기
_mc는 매 프레임마다 tx, ty로 부드럽게 이동한다.
목적지에 도착하면, 매 프레임마다 할 일 제거. */
_root._mc.onMouseDown = function(){
this.tx = _root._xmouse;
this.ty = _root._ymouse;
this.count = 0;
this.onEnterFrame = function(){
this._x = this._x + 0.2 * (this.tx - this._x);
this._y = this._y + 0.2 * (this.ty - this._y);
// 30프레임 지난 후 목적지 도착하면 onEnterFrame 제거
if ( ++this.count > 30){
delete this.onEnterFrame;
}; //if
};//onEnterFrame
}; //onMouseDown
=======================================================================================
<this >
- 원칙 : 프레임을 포함하는 무비클립
- 예외 : {콜백함수 안에서}{명시적으로(this를 쓴경우) 쓴 this} 는 콜백함수를 호출한 객체를 가리킨다.
this 는 생략이 가능하다.
모든 변수는 주인을 가진다 ==> 주인은 무비클립이 된다.
_root.bird_mc.onEnterFrame = function(){
bbb = 20; ==>_root.bbb = 20;
this.bbb = 30; ==>예외의 this (_root.bird_mc.bbb = 30;)
};
=======================================================================================
* 절대경로 : _root부터 시작해서 각 무비크립들을 접근해 가는 방법
* 상대경로 : _root를 사용하지 않고 this와 _parent(현재 가리키고 있는 무비클립의 한단계 위의 무비클립)를
이용해서 접근
_root.bird_mc.onMouseDown = function(){
this.aaa = 10; ==> _root.bird_mc.aaa = 10;
this._parent.aaa = 30; ==>_root.aaa = 30;
ccc = 50; ==>_root.ccc = 50;
};
추천 : 607
추천
목록