views:

22

answers:

2

Hello, I need to take control of how my base game class updates:

package
{
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    public class Test extends Sprite
    {
        private var sprite:Sprite;

        public function Test()
        {
            sprite = new Sprite();
            sprite.graphics.beginFill(0xFF0000, 1);
            sprite.graphics.drawRect(0, 0, 20, 20);
            sprite.graphics.endFill();
            addChild(sprite);
            this.stage.addEventListener(KeyboardEvent.KEY_UP,
                moveSprite);
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,
                moveSprite);    
        }


        private function moveSprite(keyEvent:KeyboardEvent):void
        {
            switch (keyEvent.keyCode)
            {
                case 37:
                    sprite.x--
                    break;
                case 38:
                    sprite.y--;
                    break;
                case 39:
                    sprite.x++
                    break;
                case 40:
                    sprite.y++;
                    break;
                default:
                    break;
            }
        }
    }
}

Here after every key press/release AS3 takes care of automatically drawing a sprite onto screen, but I need to do this manualy. Haw to get my hands on updating mechanism here? I need something like this - all time repeating game loop that is set to be updated/redrawn every time at its end. Are there any ideas how could I achieve this?

Thanks everyone for help.

A: 

You could convert this into a MovieClip which has a timeline, then update the animation as you wish on Event.ENTER_FRAME.

package
{
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    public class Test extends MovieClip
    {
        private var sprite:Sprite;

        public function Test()
        {
            sprite = new Sprite();
            sprite.graphics.beginFill(0xFF0000, 1);
            sprite.graphics.drawRect(0, 0, 20, 20);
            sprite.graphics.endFill();
            addChild(sprite);
            this.stage.addEventListener(KeyboardEvent.KEY_UP,
                moveSprite);
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,
                moveSprite);    
            this.stage.addEventListener(Event.ENTER_FRAME,
                moveSpriteRight);    
        }


        private function moveSprite(keyEvent:KeyboardEvent):void
        {
            switch (keyEvent.keyCode)
            {
                case 37:
                    sprite.x--
                    break;
                case 38:
                    sprite.y--;
                    break;
                case 39:
                    sprite.x++
                    break;
                case 40:
                    sprite.y++;
                    break;
                default:
                    break;
            }
        }
        private function moveSpriteRight(e:Event):void
        {
          sprite.x++;
        }
    }
}
Sandro
A: 

You can also use a Timer loop for these sorts of things:

package
{
  import flash.display.Sprite;
  import flash.utils.Timer;
  import flash.events.TimerEvent;

 public class Game
 {
   private var _timerTickRate : int = 500; //milliseconds
   private var _timer : Timer;

   public function Game()
   {
       init();
   }

   private function init() : void
   {
       _timer = new Timer( _timerTickRate );
       _timer.addEventListener( TimerEvent.TIMER, onTimerTick );
       _timer.start(); //to stop, call _timer.stop() and remove this listener
   }

   private function onTimerTick( event : TimerEvent ) : void
   {
      //do game loop stuff here.
   }
 }
}
grumblebee