In this tutorial I will show you how to make your game character shoot missles (or whatever object you want your character to shoot). This is mainly useful for making a shooting game, but it can also be used in other types of games where shooting is not the main focus. I will show you how to shoot with the spacebar and also how to shoot on mouse click.
If you have not already created a game character Player with one of my other tutorials, visit either Control Character with Mouse or Move Character with Arrow Keys, depending on how you want to control your game character.
In your flash file, draw whatever object you would like to be your bullet.
Select the image and convert it to a movie clip: Modify> Convert to Symbol
Name it Bullet, select Movie Clip as the type, under Linkage select export for actionscript and give it an Identifier and Class name Bullet. Click okay to return to the stage.
Now, create a new actionscript file: File> New> ActionScript File
Name it Bullet and save it in the same folder as your .fla file.
In this tutorial, the bullets will shoot vertically and be removed when they reach the top of the stage.
Here is the code for our Bullet class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Bullet extends MovieClip { var speed; function onLoad() { speed = 30; } function onEnterFrame() { _y -= speed; if(_y < -10) { this.removeMovieClip(); } } } |
Line 3: We name a variable speed that will control how fast the bullet moves.
Line 5-8:When the Bullet movie clip loads, the variable speed is set to 20.
Line 12: Under onEnterFrame, the y coordinate of the Bullet movie clip decreases by the amount of the variable speed (so the bullet shoots vertically).
Line 13-16: If the y coordinate of the Bullet is less than -10, the Bullet is removed from the stage. We remove it once it is out of sight so we don’t see it pop off the stage and so it doesn’t go on infinitely.
In the Bullet class we program how the Bullet movie clip will behave once it is on the stage, but we still need to attach it to the stage when the spacebar is pressed or the mouse is clicked.
Shooting on Spacebar Press
If you have programmed arrow key movement for your game character, you will want to fire bullets by pressing the spacebar.
You will have to decide how close together the bullets can be fired to prevent a continuous stream when the spacebar is held down.
Here the bullets are spaced out by creating a timer that only allows a bullet to be fired every four frames.
Here is the code we need to add to our Player class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Player extends MovieClip { var shootSpacer; function onLoad() { shootSpacer = 0; } function onEnterFrame() { shootSpacer += 1; if( Key.isDown(Key.SPACE) && shootSpacer> 4) { shootSpacer = 0; var bullet = _root.attachMovie("Bullet","Bullet" + _root.getNextHighestDepth(), _root.getNextHighestDepth()); bullet._x = _x; bullet._y = _y; } } } |
Line 3: We name a variable shootSpacer that will act as a timer to space out the bullets.
Line 7: When the movie clip is loaded shootSpacer will be set to zero.
Line 13: Under onEnterFrame() the shootSpacer is increased by one.
Line 15-18: If the spacebar is pressed and the timer shootSpacer has reached a number greater than four, then the timer will be reset and a Bullet movie clip is attached to the stage.
Line 19-20: The bullet is attached to the Player movie clip at its 0,0 coordinates.
If you want to attach the bullet to a different part of the Player movie clip you can add or subtract pixels from the _x and _y in lines 19 and 20.
Shooting on Mouse Click
If you have programmed mouse control for your game character, you probably want to shoot on the click of the mouse button.
After programming the Bullet class as explained above, you will need to add code to your existing Player class.
Add the code in the onEnterFrame() event handler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Player extends MovieClip { function onEnterFrame() { onMouseDown= function() { var bullet = _root.attachMovie("Bullet","Bullet" + _root.getNextHighestDepth(), _root.getNextHighestDepth()); bullet._x = _x; bullet._y = _y; } } } |
Line 6-8: When the mouse is pressed, a Bullet movie clip is named as a variable bullet and is attached to the Player.
Line 9-10: The bullet’s x and y coordinates are same as the 0,0 location of the Player movie clip when it is attached.
As explained above, you can also adjust the position of the bullet by adding or subtracting from the _y and _y coordinated of the Player in Lines 9 and 10.
After it is attached, the Bullet behaves according to the programming of the Bullet class.
You can also limit the spacing of the bullets by adding the shootSpacer code that is given above in section about shooting with the spacebar.
I will cover collision testing and hitting enemies or targets in another tutorial.
Here is the game character shooting bullets … or apples, same thing.