Link to home
Start Free TrialLog in
Avatar of anotherfn
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/framesForRot;
                  rotationTimer = new Timer(1000/fps,framesForRot);
            }
            else
            {
                  isRotationConstant = true;
                  framesForRot = fps/rotationsPerSecond;
                  rotStep = angleToCompletion/framesForRot;
                  if(rotationTimer == null)
                  {
                        rotationTimer = new Timer(1000/fps);
                  }
            }
            isRotating = true;
            if(rotationTimer.running == false)
            {
                  rotationTimer.addEventListener(TimerEvent.TIMER, 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.removeEventListener(TimerEvent.TIMER, rotateToPoint);
                  }
            }
      }
}

public function stopRotating():void{
                  try{
                        isRotating = false;
                        rotationTimer.stop();
                        rotationTimer.removeEventListener(TimerEvent.TIMER, 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?
ASKER CERTIFIED SOLUTION
Avatar of CyanBlue
CyanBlue
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial