First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing! | Flash Jobs
Search tutorials
Author: David Doull | Website: http://www.artifactinteractive.com.au

Keyboard movement

Next we are going to add the code for detecting the keyboard. We will let the user control the spaceship using the arrow keys. Up arrow for up, left arrow for left etc.
Under the onClipEvent(load) code type the following.

onClipEvent (enterFrame) {

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

Now you should be able to use the arrow keys to move your spaceship smoothly around the screen. But what did those lines of code do?

The code is within an enterFrame clip event, which means that every time the movie clip enters a new frame the code is run. Effectively the code is run over and over again in a loop unless we remove or stop the movie clip.

But what key was pressed?

The first if statement checks if the key pressed is the right arrow (referenced as Key.RIGHT). If the right arrow was pressed then the code this._x+=moveSpeed; is run, otherwise it checks if the left arrow was pressed and if it was pressed the code this._x-=moveSpeed; is run.

The result of this code is to set a new x-position for the spaceship. If the right arrow is being pressed then this new x-position will be greater than the previous position, if the left arrow is being pressed then the new x-position will be less. How does it work?

this._x is a reference to the x-position of the associated movie clip.

In flash 4 you would have used a set Property command to set the x-position of a movie clip. In flash 5 you can do it by simply using _x. The word 'this' refers to the movie clip containing the script. So a line like this._x=10; would set the x-position of the associated movie clip to 10.

The += bit means 'add to the current value'. For example: this._x+=10; is exactly the same as this._x=this._x+10;

Simlialry the -= bit means 'subtract from the current value'. For example: this._x-=10; is exactly the same as this._x=this._x-10;

So why use += and -=? Because its less typing and its a popular shorthand that programmers use - hence its worth learning it so you can understand when other programmers use it.

 

Lastly the moveSpeed bit. Remember we set moveSpeed equal to a fixed value of 10 earlier. So if the left arrow was pressed the x-position would be set to the previous x-position -10 (ie: ten is subtracted from the previous x-position).

If the right arrow key is being pressed then the new x-position is the previous x-position plus 10.

so the code:

if (Key.isDown(Key.RIGHT)) { 

this._x+=moveSpeed; }

else if (Key.isDown(Key.LEFT)) {
this._x-=moveSpeed; }

means: " if the right arrow key is being pressed down then increase the x-position of the current movie clip (the spaceship) by the value of MoveSpeed (which is 10),
Otherwise if the left arrow key is beign pressed down then decreasse the x-position of the current movie clip (the spaceship) by the value of MoveSpeed (which is 10) "

Finally, the code:

if (Key.isDown(Key.DOWN)) { 
  this._y+=moveSpeed; 
} else if (Key.isDown(Key.UP)) { 
  this._y-=moveSpeed; 
} 

does the same thing only it increases or decreases the y-position of the spaceship based on the UP or DOWN arrow keys.

» Level Intermediate

Added: 2001-03-20
Rating: 7.99 Votes: 1020
(10 being the highest)
» Author
No details available.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.