-
arrow key movement and gravity as2-->as3
I'm trying to make the transition between actionscript 2 and 3.
I noticed that the following code makes the object move over 10 pixels on the key-press, then waits a second to make it move more:
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, move);
function move(e:KeyboardEvent):void {
if(e.keyCode == 37) {
object.x -= 10
}
}
I want the constant movement to start immediately.
Moreover, the following code is only executed once, instead of every frame like in actionscript 2:
I want it to be executed every frame.
Anyone know how to make it do those two things?
-
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.
-
Thanks
Thanks, that will be useful for me for a long time.
And you write Actionscript 3.0 like it's your first language; there are no errors!
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|