A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: arrow key movement and gravity as2-->as3

Threaded View

  1. #2
    I do something like this.

    Code:
    var movingLeft:Boolean = false;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
    
    //will cause the function enterFrameHandler to be fired every frame
    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    
    //will execute when any key in pushed down
    function keyDown(e:KeyboardEvent):void {
    if(e.keyCode == 37) {
       movingLeft = true;
    }
    }
    
    //will execute when any key is let go, up
    function keyUp(e:KeyboardEvent):void {
    if(e.keyCode == 37) {
       movingLeft = false;
    }
    }
    
    //is executed every frame
    function enterFrameHandler(event:Event):void{
      if(movingLeft == true){
         object.x -= 10;
      }
       //I usually check if the object at object.y + 10 has collided with a tile
       //before moving the object down.
       object.y += 10;
    }
    By doing the above the you get a smooth transition each frame based on user interaction.
    Above code wasn't tested.
    Last edited by tariqm; 08-24-2009 at 07:00 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center