moving a mc with mouse events and timers

  • chopficaro
  • Novice
  • Novice
  • chopficaro
  • Posts: 30

Post 3+ Months Ago

im seeing some very strange behavior, draw a block on the stage and make it a movie clip and ull see what i mean. sometimes the block moves 5 pixels and stops, other times it moves untill it leaves the stage.

Code: [ Select ]
package
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    public class FunButton extends MovieClip
    {
    var destinationX:Number = new Number();
    var origX:Number = new Number();
        var moveLeftTimer:Timer = new Timer(0,0);
        var moveRightTimer:Timer = new Timer(0,0);
        public function FunButton()
        {
            var destinationX:Number = this.x + 50;
            var origX:Number = this.x;
            this.addEventListener(MouseEvent.MOUSE_OVER, TimerRight);
            this.addEventListener(MouseEvent.MOUSE_OUT, TimerLeft);
        }
        
        private function TimerRight(event:MouseEvent):void
        {
            var numTimer:Number = int((destinationX - this.x)/5);
            var moveRightTimer = new Timer(50,numTimer);
            if(moveLeftTimer.running){ moveLeftTimer.stop(); }
            if(moveRightTimer.running){ moveRightTimer.stop(); }
            moveRightTimer.addEventListener(TimerEvent.TIMER, MoveRight);
            moveRightTimer.start();

        }
        
        private function TimerLeft(event:MouseEvent):void
        {
            var numTimer:Number = int((this.x - origX)/5);
            var moveLeftTimer = new Timer(50,numTimer);
            if(moveLeftTimer.running){ moveLeftTimer.stop(); }
            if(moveRightTimer.running){ moveRightTimer.stop(); }
            moveLeftTimer.addEventListener(TimerEvent.TIMER, MoveLeft);
            moveLeftTimer.start();
        }
        
        private function MoveRight(event:TimerEvent):void
        {
            this.x += 5;
        }
        
        private function MoveLeft(event:TimerEvent):void
        {
            this.x -= 5;
        }
    }
}
  1. package
  2. {
  3.     import flash.display.MovieClip;
  4.     import flash.events.MouseEvent;
  5.     import flash.events.TimerEvent;
  6.     import flash.utils.Timer;
  7.     public class FunButton extends MovieClip
  8.     {
  9.     var destinationX:Number = new Number();
  10.     var origX:Number = new Number();
  11.         var moveLeftTimer:Timer = new Timer(0,0);
  12.         var moveRightTimer:Timer = new Timer(0,0);
  13.         public function FunButton()
  14.         {
  15.             var destinationX:Number = this.x + 50;
  16.             var origX:Number = this.x;
  17.             this.addEventListener(MouseEvent.MOUSE_OVER, TimerRight);
  18.             this.addEventListener(MouseEvent.MOUSE_OUT, TimerLeft);
  19.         }
  20.         
  21.         private function TimerRight(event:MouseEvent):void
  22.         {
  23.             var numTimer:Number = int((destinationX - this.x)/5);
  24.             var moveRightTimer = new Timer(50,numTimer);
  25.             if(moveLeftTimer.running){ moveLeftTimer.stop(); }
  26.             if(moveRightTimer.running){ moveRightTimer.stop(); }
  27.             moveRightTimer.addEventListener(TimerEvent.TIMER, MoveRight);
  28.             moveRightTimer.start();
  29.         }
  30.         
  31.         private function TimerLeft(event:MouseEvent):void
  32.         {
  33.             var numTimer:Number = int((this.x - origX)/5);
  34.             var moveLeftTimer = new Timer(50,numTimer);
  35.             if(moveLeftTimer.running){ moveLeftTimer.stop(); }
  36.             if(moveRightTimer.running){ moveRightTimer.stop(); }
  37.             moveLeftTimer.addEventListener(TimerEvent.TIMER, MoveLeft);
  38.             moveLeftTimer.start();
  39.         }
  40.         
  41.         private function MoveRight(event:TimerEvent):void
  42.         {
  43.             this.x += 5;
  44.         }
  45.         
  46.         private function MoveLeft(event:TimerEvent):void
  47.         {
  48.             this.x -= 5;
  49.         }
  50.     }
  51. }
  • chopficaro
  • Novice
  • Novice
  • chopficaro
  • Posts: 30

Post 3+ Months Ago

this code is supposed to, on mouse over, move a mc 50 pixels to the right in 10 frames, and on mouse out, move it back. im also trying to take into consideration what happenes if the mouse event occurs during the movement

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 1 guest
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.