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.












