can someone help me to transform this code from as2 to as3?
For a simple circle, i want when i go with mouse cursor to right , the circle to rotate (don't need to move my mouse cursor but the circle still rotating)
I know that _root._xmouse
goes t mouseX and this._rotation
goes to 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 version:
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;
}
This should work :
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;
}
notes / tips :
Declare variables outside of functions where possible.
The evt
in ( evt : Event )
is your target reference to whatever has the .addEventListener(MouseEvent.MOUSE_MOVE)
attached to it. So if you want to move more than one thing, just give them all the same addEvent
same like rota.addEvent...
but as you can see the function moves only rota
at present, so by changing code to using evt.rotation
and evt.mouseX
etc... the evt
now makes it universal to anything listening to that mouseMoving
function.
Edit (based on comments) :
The variable speed
sets speed of rotation. For rotation
set the direction by either -=
or +=
.
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; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.