有人可以帮助我将此代码从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;
}
答案 0 :(得分: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...
相同,但正如您所看到的那样,函数目前仅移动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; }
}