actionscript 2 to actionscript 3 my code



有人可以帮助我将这段代码从 as2 转换为 as3 吗?

对于一个简单的圆圈,我希望当我用鼠标光标向右移动时,圆圈要旋转(不需要移动我的鼠标光标,但圆圈仍在旋转(

我知道_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;
}

这应该有效:

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 : Event )中的evt是附加了.addEventListener(MouseEvent.MOUSE_MOVE)的任何内容的目标引用。因此,如果您想移动多个东西,只需像rota.addEvent...一样给它们相同的addEvent但正如您所看到的,该函数目前仅移动rota,因此通过将代码更改为使用 evt.rotationevt.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; }
}

最新更新