2017-04-07 71 views
0

有人能帮我把这个代码从as2转换成as3吗?actionscript 2到actionscript 3我的代码

对于一个简单的圆形,我希望当我与鼠标光标移动到右边去,圆旋转(不需要移动我的鼠标光标但仍一圈旋转)

我知道_root._xmouse去ŧ mouseX和this._rotationthis.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; 
} 
+0

_“不需要移动我的鼠标光标,但圆形仍在旋转”_...这就是'EnterFrame'所做的。它以您的SWF的FPS速率重复代码。也许你想要一个'Mouse_Move'侦听器中的代码逻辑?显示您尝试制作的AS3版本代码,更容易帮助您解决问题。 –

+0

是的,也许是一个mouse_move监听器,这个as2代码如何工作(as3代码)...? – romania

+0

我们需要查看迄今为止您所拥有的AS3代码,以显示如何应用as2逻辑。例如:没有人知道你的圈子变量名等等。你可以用你的Circle变量名称替换this,并将其用作'circleName.rotation = circleName.rotation%180;'etc etc –

回答

0

这应该工作:

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...10一样,但是正如你所看到的,该功能目前只能移动rota,所以通过改变代码来使用evt.rotationevt.mouseX etc ... 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; } 

} 
+0

我喜欢这个谢谢! ,以及我如何做,如果我希望该圈继续向右移动,如果我去鼠标右侧,然后我停止移动鼠标,但圆仍然旋转到右侧(同样的事情左) – romania

+1

谢谢,但你能**不接受**现在的答案?我试图让我的代表得分为'6-6-6-6'给朋友开玩笑,而'✓'已经把它推得太过分了。你可以在周末或其他时间重新喜欢。 –

+1

完成! ,所以我怎么能做到我之前说的?如果很简单.. – romania