Space Shooter - Enemies
| Category | : Flash | Views | : 23674 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : space-shooter-enemies.zip | ||
| Result | : See the result | ||||
| <<< Previous | Index Series | Next >>> |
This tutorial is a series of tutorials that will teach you how to create Space Shooter Game. The tutorial is divided in 5 parts. If you haven't learned the previous tutorials, please see Space Shooter Flash Game
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 Movie Clip and draw your enemy ship, don't forget to set the identifier to "Enemy" and select "Export to Actionscript".
Now drag the enemy Movie Clip 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 Movie Clip 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.
![]() |
![]() |
![]() |
![]() |
![]() |
| <<< Previous | Index Series | Next >>> |




