Space Shooter - Shooting
| Category | : Flash | Views | : 30309 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : space-shooter-shooting.zip | ||
| Result | : See the result | ||||
| <<< Previous | Index Series | Next >>> |
First we create a variable "i = 0", it is used to give different name to every bullet.
If the space bar is pressed we increase i by 1, attach the bullet Movie Clip and set the coordinates for our bullet.
i++; //Increase the variable i by 1.
_root.attachMovie("Bullet", "Bullet" + i, _root.getNextHighestDepth());
This is a function to attach a Movie Clip from the library, its parameters are:
_root.attachMovie(identifier, new name, depth);
The "identifier" is "Bullet" that we set when creating our Movie Clip.The "new name" is "Bullet" + i, so the name is "Bullet1" if i = 1, or "Bullet2" if i = 2.
The "depth" is in which "layer" the new Movie Clip instance is made, because of that we can't have two instances of "Bullet" with the same depth or one would overwrite the other. So we use _root.getNextHighestDepth() and flash automatically gets a free depth for us.
_root["Bullet" + i]._x = Ship._x + 3;
_root["Bullet" + i]._y = Ship._y;
This code sets the new Bullet coordinates so it starts in front of our ship.Before we finish this tutorial, let's put some limits to where our ship can go, we don't want it to get off the screen. Make these changes:
if (Key.isDown(Key.RIGHT))
{
if (Ship.hitTest(550, Ship._y, true))
{
Ship._x -= 5;
}
Ship._x += 5;
} else if (Key.isDown(Key.LEFT))
{
if (Ship.hitTest(0, Ship._y, true))
{
Ship._x += 5;
}
Ship._x -= 5;
} else if (Key.isDown(Key.UP))
{
if (Ship.hitTest(Ship._x - 40, 0, true))
{
Ship._y += 5;
}
Ship._y -= 5;
} else if (Key.isDown(Key.DOWN))
{
if (Ship.hitTest(Ship._x - 40, 300, true))
{
Ship._y -= 5;
}
Ship._y += 5;
}
if (Ship.hitTest(550, Ship._y, true))
{
Ship._x -= 5;
}
This is a hitTest() function, it has these parameters:
hitTest(x, y, true)
If our ship hits our limit, we decrease its position by the same amount of the speed, so the Ship will never leave the screen.Done! The ship should be shooting now, press Ctrl + Enter and test the game.
We continue our game in the next tutorial, where we will make an enemy ship and a simple AI.
![]() |
![]() |
![]() |
![]() |
![]() |
| <<< Previous | Index Series | Next >>> |




