In this tutorial we will look at placing a 'player' character on screen and allowing a user to move that character around the screen using the keyboard arrow keys.
In order to achieve player movement, there are two things we have to think about - how to detect keyboard presses and how to move the player character.
This is quite similar to attaching code to a button. The main difference is that the code must occur in what is called a 'clip event'. The code inside a clip event will only run when that clip event occurs. Just like a button's code will only run when the mouse clicks the button. The most frequently used clip event is 'enterFrame'. This is used when we want the code to run continuously, i.e. it runs every time a frame starts playing (which is continuously in a Flash movie).
Okay, lets set up an 'enterFrame' clip event, attached to the player character. To do this, add the following code to the Actions Panel.
onClipEvent(enterFrame){
}
Any code that we want to run continuously (at the frame rate of the Flash movie) must be placed inside the curly brackets.
if (Key.isDown(Key.UP)){
}
Here is the code to do that - place it inside the curly brackets of the Key.isDown() method.
this._y-=10;
In summary, here is the entire code that should be attached to the player character instance:
onClipEvent(enterFrame){
if (Key.isDown(Key.UP)){
this._y-=10;
}
}