This is the second part of the tutorial and we are going to enable the ship to shoot.
Previously we made our ship fly; now we are going to make it shoot and set limits to where it can go.
First we need to make our bullets, right click the library tab, select "New Symbol..." and give it the name "Bullet".
Before clicking OK, click Advanced, select "Export for Actionscript" and make sure the identifier is "Bullet".
We do this because we need to export the Bullet Movie Clip during running time to create many bullets and we will use the name determined in identifier to call it later.
Draw a small bullet, and position it like this:
This is called registration point, in the stage this is the position of the Bullet MC.
Now open the actions window of the first frame of the MC and paste this code:
| this.onEnterFrame = function() { this._x += 12; if (this._x > 550) { this.removeMovieClip(); } } |
"this" is the MC instance, so if we duplicate 100 bullets each bullet will have the same code.
| this._x += 12; |
Every frame, the bullet will move 12 pixels to the right.
| if (this._x > 550) { this.removeMovieClip(); } |
if the bullet passed the limits of the screen (550px) it's no longer visible, so we remove it by using this method "removeMovieClip()"
Now go to the main timeline, select the first frame and open the actions windows. You will see the previous codes that we have written. Add this code to the old one so we can fire our bullets:
| var i = 0; this.onEnterFrame = function() { . . . else if (Key.isDown(Key.DOWN)) { Ship._y += 5; } if (Key.isDown(Key.SPACE)) { i++; _root.attachMovie("Bullet", "Bullet" + i, _root.getNextHighestDepth()); _root["Bullet" + i]._x = Ship._x + 3; _root["Bullet" + i]._y = Ship._y; } } |
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 MC 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 MC from the library, its parameters are:
| _root.attachMovie(identifier, new name, depth); |
The "identifier" is "Bullet" that we set when creating our MC.
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 MC 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.






