Internet Commerce

Partners & Affiliates

Developer Channel


Featured Flash FLA
Gallery Downloads 11401 Flash Movies | 5 New Flash Movies Added
What's New | Top 100

Featured FLA

»  Author: Nick Kouvaris
»  Title: Znax
»  Description: Znax is a board game. Click 4 tiles of the same color and form squares as big as you can. You will erase all the tiles inside the square and collect points. Get maximum score if you make a square with game edges.
»  More by: Nick Kouvaris


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 4941 Flash Sites | 1 New Flash Links
What's New | Top 100 Flash Site

Featured Site

»  Author Agence WOP Digital Agency
»  Title: Electricdrum
»  Description: French WOP Agency, 3D websites, Flash (Papervision, Away 3D), event or institutional projects. The agency operates on all digital projects: consulting, design, graphic design, development, online communication. The WOP agency follows you on the implementation of original, creative and optimized digital projects.


Random Links | Add your own Flash Related Links
Flash Tutorials 1481 Tutorials 7 New Tutorials Added!
What's New | Top100

» How To Make A Simple Animation Using Christmas Clips
» Simple Step by step flash game tutorial Spot the diffrence
» How To Make A Moving Text Slide
» Create Flash Banner With Text Float Effect
» How To Make Zoo Photos Slideshow
» How To Make A Dolphin Photos Slideshow
» How To Make A Fathers Day Slideshow
» How To Make A Transparent Background of Your Flash File
» Create Flash Banner With Text Disco Light Effect Today we will introduce you a Text Disco Light eff
» Unknown Tag: Title10
Random Tutorial | Add Site


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Games

Categories Building Games in Flash5 Part1: Player Movement and Fire
Author: David Doull | Website: http://www.artifactinteractive.com.au |

 
Page 4
«prev 1 2 3 4 5 next»

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.

«prev 1 2 3 4 5 next»

» Level Intermediate

Added: : 2001-03-20
Rating: 8.22 Votes: 1024
Hits: 9490
» Author
No details available.
» Download
Download the files used in this tutorial.
Download (72 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments