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: << < 1  2  3  4  5  > >>

This is the third tutorial of a series about developing a Space Shooter game, in this tutorial we are going to make enemy ships.

Create a new MC and draw your enemy ship, don't forget to set the identifier to "Enemy" and select "Export to Actionscript".

Now drag the enemy MC to the stage, give it an instance name of "Enemy0", right click it and select "Actions". Paste this:

onClipEvent(load) { function reset() { var timer = 12; this._y = Math.random() * 300 this._x = 550 mySpeed = Math.ceil(Math.random() * 6) + 1; } reset(); }

This part of the code creates a function called reset() when the MC loads for the first time.

Inside reset() we set this._y to a random number between 0 and 300, this._x to 550 and the speed of our enemy to a random number between 1 and 6.

And paste this:

onClipEvent(enterFrame) { //in every frame the enemy move left in the speed defined in the reset function. this._x -= mySpeed; if (this._x < -10) { //if the enemy is not on the screen, we reset it. reset(); } //if our timer is bigger than 12 we get a new if (timer >= 12) { //direction to the Ship. var dir = Math.ceil(Math.random() * 2) //We get a random number, 1 or 2. 1 is up, 2 is down. //Then we set timer to 0, so we only get a new dir when timer is bigger than 12 timer = 0; } if (dir == 1) { //if dir = 1, we move the ship up. this._y -= 3; } else if(dir == 2) { //if dir = 2, we move the ship down. this._y += 3; } //increase timer by 1, so the timer gets equal to 12 and we get a new direction. timer++ }

This is a VERY basic AI that will be improved when we let the enemy know when it killed our ship or if it was killed by us.

We only have one enemy, so let's make more.

Add this code in the main timeline before the rest of the code.

var nrEnemies = 3; for (i = 1; i < nrEnemies; i++) { _root.Enemy.duplicateMovieClip("Enemy" + i, _root.getNextHighestDepth()); }

The variable nrEnemies represents the number of enemies in the screen at the same time.

for (i = 1; i < nrEnemies; i++) { ... }

The "for loop" will run the code inside it depending on nrEnemies.

_root.Enemy.duplicateMovieClip("Enemy" + i, _root.getNextHighestDepth());

This duplicate the MC Enemy into a new MC called ("Enemy" + i) and gives this new MC the highest depth available.

These new MC's will have the same code we did previously to make the enemy move and "think".

Lets make some collision checks to kill our enemies and for them to kill us.

Open the Bullet MC code, and add this inside "onEnterFrame":

for (i = 0; i < _root.nrEnemies; i++) { if (this.hitTest(_root["Enemy" + i])) { _root["Enemy" + i].reset(); this.removeMovieClip(); } }

This code uses the hitTest() function to check if the bullet has collided with one of the enemies and if it did collided the function reset() is called, so the enemy doesn't actually die, he just gets a new position and speed.

Now right click the Ship instance and select "Actions".

This sets the function reset() of the ship, we will call it when our ship is destroyed.

onClipEvent(load) { function reset() { this._x = 100; this._y = 150; } reset() }

Paste this code to check collision between the ship and the enemy:

onClipEvent(enterFrame) { for (i = 0; i < _root.nrEnemies; i++) { if (this.hitTest(_root["Enemy" + i])) { reset() for(k = 0; k < _root.nrEnemies; k++) { _root["Enemy" + k].reset(); } } } }

Again we use the hitTest() function, this time with different parameters:

//"this" is the Ship MC //"target" is the Enemy MC this.hitTest(target);

The difference is that this one doesn't care to where the MC's are colliding, the other used a _x and _y coordinate to check collision.

That's it for this tutorial. In the next one we are going to make score, lives and a Game Over screen.



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: << < 1  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