Key.isDown

Key.isDown(Key.UP)

Create the onEnterFrame function. This one is pre-defined in ActionScript, so spelling and case-sensitivity count:

onEnterFrame function

Remember, any actions we make part of the onEnterFrame function will repeat at (about) the frame rate. So, if your frame rate is 24fps, actions will repeat (about) twenty-four times every second. (I say "about" because, in practice, it isn't exactly at the frame rate)

Now we'll create a "listener" that checks to see if a user has pressed the UP key. Let's start with that key word: if. We need to create an if statement:

function onEnterFrame (){
if( ){
}
}

For our condition, we'll use the built in Key.isDown() function:

function onEnterFrame (){
if(Key.isDown()) {
}
}

OK, we're checking to see if a key is down, but which key? Let's listen for the UP key. We use Key.UP and you better believe it is case sensitive:

function onEnterFrame (){
if(Key.isDown(Key.UP)) {
}
}

The Actions window should now look like this:

ifStatement

That's the "IF" part of our conditional statement, now we have to tell the program what to do if the condition is true (the "THEN"). When the user presses the UP key, we want the ship to move up.... so the y value has to get smaller.

We'll decrement ship._y by five to start. We could write this statement:

ship._y = ship._y-5;

...but let's take a little programming shortcut:

ship._y-=5;

Now this block of code looks professional:

OK, time to test it out! Your ship should move up five pixels every time you press the UP key:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player