A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11336 Flash Movies | 2 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Surjit Dhami
» Title: Book
» Description: Book
» More by Surjit Dhami


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

Featured Site

» Posted in the Flash Kit Links section
» Title: All-American Rejects
» Description: Get to know this great band by exploring their "practice room".


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

» Create xml slideshow with free template
» How to Insert a Multilingual Subtitle Into Your Flash Video Studio
» How to Create Cool Halloween Slideshow
» Debugging flash using the Firebug console
» Create Flash Slideshow on Blogger
» FLASH TRICKS IN WEB ADVERTISING: FLASH BANNERS
» HTML Photo Gallery Tutorial
» Create your first flash site – PART 1
» How to Make a Flash Photo Gallery
» Unknown Tag: Title10
Random Tutorial | Add Site

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume


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.21 Votes: 1005
Hits: 9483
» 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
 
   
 

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs