Our Flash tutorials provide you with step-by-step instructions on how to create beautiful Flash animations and integrate them into your website.  Home Flash & Swish Flash Tutorials Space Shooter - Movie Clip and Movement

Space Shooter - Movie Clip and Movement

Browse Pages: 2  3  4  5  > >>

This is the first part of a series of Space Shooter tutorials. In this part we are going to learn the basics of Movie Clips and character movement.

First create a new document in Flash, click "File" > "New...", select "ActionScript 2.0" and click Ok.

Create a Movie Clip by right clicking your library tab and selecting "New Symbol...", make sure "Movie Clip" is selected and choose a name.

image 1

Flash will create and open the MC for editing, so just paste an image or draw a space ship with Flash's drawing tools.

After drawing your ship, get back to the main timeline by click on "Scene 1":

image 2

Now drag your MC from the library to the Stage, select it and give it an instance name.

Space Shooter - Movie Clip and Movement

I am going to name it "Ship". So "Ship" is how I am going to call this MC instance inside my code.

We just made our ship, so let's make it move.

Right click the first frame in your timeline and select "Actions". Type this:

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

Press Ctrl + Enter and test it, use the arrow key to move your ship around.

Here is how this code works:

this.onEnterFrame = function() { ... }

This is a very simple event from flash; the code inside it will be executed every time flash enters this frame.

if (Key.isDown(Key.RIGHT)) { Ship._x += 10; //_x -= 10 would move the ship to the left. }

This check if the right key is pressed, then move the Ship 10 pixels to the right.

else if (Key.isDown(Key.Up)) { Ship._y -= 10; // _y += 10 would move the ship down. }

If the up arrow key is pressed, the ship will move up.

It's the same thing as before, but this time we added "else" before "if" because we don't want the ship to move in two directions at the same time.

In case you want the ship to move freely remove the "else" and try to move it.

We are done with movement! In the next tutorial we are going to make our ship fire bullets.



About the Author:

Webzo Studio is a web design company located in the United States. Webzo Studio was founded in August 2005. We are a small team of designers that just love our job. The love of doing what we do is what makes us not doing our job as a job, but as an enjoyment.
read more about this author

Author's URL: Webzo Studio
Browse Pages: 2  3  4  5  > >>
Final results of our readers
New!
Passed through all the steps? Share your result!
Your result will be premoderated.
Please make sure you choose the right image.
 
 



Captcha

*Required fileds
Our Flash tutorials provide you with step-by-step instructions on how to create beautiful Flash animations and integrate them into your website. More Flash Tutorials: Featured Materials | Fresh Materials | More Flash Tutorials at FlashPerfection.com

Reader's comments
comments Joey December 07, 2011 says:
I DONT WORK. Could you please find your error in the coding?
Reply
comments MaryA2900 April 13, 2011 says:
Hi, I am confused on what replaces the word 'this' or if it stays the same.
Reply
comments Tammie October 14, 2010 says:
I can't get this page to work either. It is very confusing of where to paste the code.
Reply
comments Tommy March 24, 2010 says:
hey i got to the of this page and i cant go any further because i keep on recieving this error when i try to test my movie
it looks like this Operator '=' must be followed by an operand now this happened when i pasted the actionscript for the bullets
can you help me out?

Reply
Add comments to "Space Shooter - Movie Clip and Movement"

Captcha