Space Shooter - Enemies

Category : Flash Views : 24009
Version : 8 Rating
3.6/5 (49 votes)
Type : Text Source File: space-shooter-enemies.zip
Result: See the result


<<< PreviousIndex SeriesNext >>>

Previous | 1 | 2 | Next

_root.Enemy.duplicateMovieClip("Enemy" + i, _root.getNextHighestDepth());
This duplicate the Movie Clip Enemy into a new Movie Clip called ("Enemy" + i) and gives this new Movie Clip the highest depth available.
These new Movie Clip'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 Movie Clip 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 Movie Clip //"target" is the Enemy Movie Clip this.hitTest(target);
The difference is that this one doesn't care to where the Movie Clip'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.

del.icio.us digg it Reddit Stumble Upon Technorati

Previous | 1 | 2 | Next

<<< PreviousIndex SeriesNext >>>




Supporters

Reseller Web Hosting