anotherfn
asked on
AS3 Timer problem
We are developing a project in pure AS3, and are having a problem with a Timer object. We have an icon that spins as we wait for a user to take an action, and to make the rotation animate, we are using the following code:
public function setUpRotate(rotateByAngle: Number, timeInSecs:Number, fps:int, rotationsPerSecond:Number = 0):void{
if(!isRotating)
{
var angleToCompletion:Number = rotateByAngle - this.rotationZ;
currentRotationFrame = 0;
if(timeInSecs != 0)
{
isRotationConstant = false;
framesForRot = timeInSecs*fps;
rotStep = angleToCompletion/framesFo rRot;
rotationTimer = new Timer(1000/fps,framesForRo t);
}
else
{
isRotationConstant = true;
framesForRot = fps/rotationsPerSecond;
rotStep = angleToCompletion/framesFo rRot;
if(rotationTimer == null)
{
rotationTimer = new Timer(1000/fps);
}
}
isRotating = true;
if(rotationTimer.running == false)
{
rotationTimer.addEventList ener(Timer Event.TIME R, rotateToPoint);
rotationTimer.start();
}
}
}
protected function rotateToPoint(e:TimerEvent ):void{
if(isRotating)
{
this.rotationZ += rotStep;
currentRotationFrame ++;
if(isRotationConstant == false)
{
if(currentRotationFrame >= framesForRot)
{
isRotating = false;
rotationTimer.stop();
rotationTimer.removeEventL istener(Ti merEvent.T IMER, rotateToPoint);
}
}
}
}
public function stopRotating():void{
try{
isRotating = false;
rotationTimer.stop();
rotationTimer.removeEventL istener(Ti merEvent.T IMER, rotateToPoint);
}
catch(e:Error)
{
//error handling
}
}
This code works fine the first time we call it, but from the second time onward, whenever it is waiting for the user action, it goes faster and faster. We also have a separate stopRotating function for when the object is rotating indefinitely, so that it doesn't keep going, but this doesn't seem to be working. How do we avoid the sprite rotating faster each time a rotation begins, as we need it to always be the same speed?
public function setUpRotate(rotateByAngle:
if(!isRotating)
{
var angleToCompletion:Number = rotateByAngle - this.rotationZ;
currentRotationFrame = 0;
if(timeInSecs != 0)
{
isRotationConstant = false;
framesForRot = timeInSecs*fps;
rotStep = angleToCompletion/framesFo
rotationTimer = new Timer(1000/fps,framesForRo
}
else
{
isRotationConstant = true;
framesForRot = fps/rotationsPerSecond;
rotStep = angleToCompletion/framesFo
if(rotationTimer == null)
{
rotationTimer = new Timer(1000/fps);
}
}
isRotating = true;
if(rotationTimer.running == false)
{
rotationTimer.addEventList
rotationTimer.start();
}
}
}
protected function rotateToPoint(e:TimerEvent
if(isRotating)
{
this.rotationZ += rotStep;
currentRotationFrame ++;
if(isRotationConstant == false)
{
if(currentRotationFrame >= framesForRot)
{
isRotating = false;
rotationTimer.stop();
rotationTimer.removeEventL
}
}
}
}
public function stopRotating():void{
try{
isRotating = false;
rotationTimer.stop();
rotationTimer.removeEventL
}
catch(e:Error)
{
//error handling
}
}
This code works fine the first time we call it, but from the second time onward, whenever it is waiting for the user action, it goes faster and faster. We also have a separate stopRotating function for when the object is rotating indefinitely, so that it doesn't keep going, but this doesn't seem to be working. How do we avoid the sprite rotating faster each time a rotation begins, as we need it to always be the same speed?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.