troubleshooting Question

AS3 Timer problem

Avatar of anotherfn
anotherfn asked on
Web DevelopmentAdobe FlashApache Flex
1 Comment1 Solution789 ViewsLast Modified:
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
CyanBlue

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros