Forums

ForumsProgramming Forum

Basic Keyboard Movement in ActionScript 3

Posted Apr 29, '08 at 11:19pm

SeoMX

SeoMX

87 posts

Does anyone know how to make stuff move using the keyboard in ActionScript 3???

 

Posted May 1, '08 at 8:07pm

ThexDancingxMuffin

ThexDancingxMuffin

64 posts

onClipEvent (load) {
    moveSpeed = ~3~;
}
onClipEvent (enterFrame) {
    if (Key.isDown(Key.~1~)) {
        this._~2~+=moveSpeed;
    }
}

EXPLAINING:
~1~: this is the key that you press to make the character make. Ex: spacebar, 2 key, G key, etc.
~2~: this is the axis that the object will move on. Y axis is horizontal and X axis is vertical.
~3~: this is the speed that it moves when you press a button. Pretty self-explanatory.

So, in the end, it would look something like this:

onClipEvent (load) {
    moveSpeed = 15;
}
onClipEvent (enterFrame) {
    if (Key.isDown(Key.RIGHT)) {
        this._x+=moveSpeed;
    }
    if (Key.isDown(Key.LEFT)) {
        this._x-=moveSpeed;
    }
    if (Key.isDown(Key.DOWN)) {
        this._y+=moveSpeed;
    }
    if (Key.isDown(Key.UP)) {
        this._y-=moveSpeed;
    }
}

 

Posted May 1, '08 at 8:08pm

ThexDancingxMuffin

ThexDancingxMuffin

64 posts

**~1~: this is the key that you press to make the character move. Ex: spacebar, 2 key, G key, etc.

 

Posted May 7, '08 at 9:22am

rwtwm

rwtwm

6 posts

is that not AS2 code?

in Actionscript 3 all key inputs are dealt with event handlers.

To use these you need two things, a function to execute when the event occurs and an event listener on the stage to listen for keyboard input.

It looks like...

function (event:KeyboardEvent){
     if(event.keyCode == Keyboard.LEFT){
           .x -=
     }
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, );

note that your movement function is also an argument of the event listener. The character name is whatever you want to move when the key is depressed.

 

Posted May 13, '08 at 12:32am

Apathy

Apathy

4 posts

There's a ton of tutorials that'll help if you if you search on any engine as well :)

 
Reply to Basic Keyboard Movement in ActionScript 3

You must be logged in to post a reply!