[英]actionscript 2 to actionscript 3 my code
有人可以幫助我將這段代碼從as2轉換為as3嗎?
對於一個簡單的圓,我想當我將鼠標光標移到右邊時,圓旋轉(不需要移動鼠標光標,但圓仍在旋轉)
我知道_root._xmouse
進入mouseX, this._rotation
進入this.DisplayObject.rotation
onClipEvent(enterFrame)
{
this.xmouse = Math.min(908, Math.max(0, _root._xmouse));
if (_root._xmouse > 0)
{
var offset = Stage.width / 2 - this.xmouse;
this._rotation = this._rotation + offset / 2000;
} else {
this._rotation = this._rotation - 0.02;
}
this._rotation = this._rotation % 180;
}
AS3版本:
stage.addEventListener( Event.ENTER_FRAME, mouseOver );
function mouseOver( e: Event ) : void
{
rota.mouseX == Math.min(908, Math.max(0, stage.mouseX));
if (stage.mouseX > 0)
{
var offset = stage.stage.width / 2 - rota.mouseX;
rota.rotation = rota.rotation + offset / 2000;
}else{
rota.rotation = rota.rotation - 0.02;
}
rota.rotation = rota.rotation % 180;
}
這應該工作:
var offset : int = 0; //declare the variable (integer)
//stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoving );
rota.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoving );
function mouseMoving ( evt : Event ) : void
{
rota.x = stage.mouseX; //Math.min(908, Math.max(0, stage.mouseX));
if (stage.mouseX > 0)
{
offset = stage.stage.width / 2 - rota.mouseX;
rota.rotation = rota.rotation + offset / 2000;
}else{
rota.rotation = rota.rotation - 0.02;
}
rota.rotation = rota.rotation % 180;
}
注意事項/提示:
盡可能在函數之外聲明變量。
該evt
在( evt : Event )
是你的目標參考任何具有.addEventListener(MouseEvent.MOUSE_MOVE)
連接到它。 所以,如果你想移動多件事情,只是給他們所有相同addEvent
同樣喜歡rota.addEvent...
但你可以看到功能只移動rota
目前通過改變代碼來使用,所以evt.rotation
和evt.mouseX
等... evt
現在使它成為偵聽mouseMoving
函數的所有對象的通用工具 。
編輯 (基於評論):
speed
設置旋轉速度。 對於rotation
,請通過-=
或+=
設置方向。
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoving ); //Stage : for direction
rota.addEventListener(Event.ENTER_FRAME, mouseRotating); //Object : for spin/rotate
var prevX:int = 0;
var currX:int = 0;
var directX: int = 0; //will update to 1=Left or 2=Right
var speed : int = 7; //speed of rotation
function mouseMoving ( evt : Event ) : void
{
prevX = currX; currX = stage.mouseX;
if (prevX > currX) { directX = 1; } //moving = LEFT
else if (prevX < currX) { directX = 2; } //moving = RIGHT
//else { directX = 0;} //moving = NONE
}
function mouseRotating ( evt : Event ) : void
{
evt.target.x = stage.mouseX; //follow mouse
if ( directX == 1 ) { evt.currentTarget.rotation -= speed; }
if ( directX == 2 ) { evt.currentTarget.rotation += speed; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.