|
|
Kabomb > Tutorials > Gun
and Bullet
Introduction
This tutorial shows you how to create a gun that fires
bullets, another step towards making a complete Flash game. |
|
|
The Code
Create a new document in Flash and set the frame rate
to about 24 fps.
Firstly, create your gun and bullet graphics by drawing
them both and making them MCs. When drawing the gun, make
sure it is facing upwards and that the center of the
MC is where the bottom of the gun is (for the point of
rotation). Making the bullet is difficult because we
have to position it so that when the player shoots, the
bullet will be at the end of the gun. To do this, we
firstly draw a circle as the bullet. Then, place a copy
of the bullet MC inside the gun MC and align it to the
middle of the stage. Then, double click on the bullet
to edit it and the gun should go a lighter colour to
show you're editing the bullet. Now, move the bullet
to where the top of the gun is, as shown in the picture
to the right. Go back to the gun MC and delete the copy
of the bullet MC that you just put there.
Go to the main timeline, open the library and select the
bullet MC. Right click or control click on it and select
'Linkage'. Check the tick box that says 'Export for Actionscript',
and type 'bullet' into the identifier text box. We do this
so that we can load this MC by using code and not have
to have it on the main timeline.
Place a copy of the gun MC on the main timeline at the
edge of the screen (wherever you want). I put mine in the
bottom middle. Then give it an instance name of 'gun'.
All you should have is one frame on the main timeline
with the gun MC on it. Select this frame and open the actions
panel. In there, paste this code:
|
Explanation
The first line sets the variable 'i' to 0, and this will
be our counter so we have original depths and names of
each bullet MC. The next function occurs every frame, where
we firstly make the variable 'i' add 1 onto it every single
frame.
Then we set the variable angle, which will determine the
angle or rotation of the gun. The 'Math.atan2' function
calculates the angle of a point given when measured counter-clockwise
from a circles x axis, when the circles mid point is at
0, 0 on the stage. In simple terms, it calculates an angle
where we want the gun to point. For the y value of that
point, we put '_root._ymouse-_root.gun._y' to find the
midpoint of the gun and the mouse and similar for the x
value. So the gun is not told to point to the mouse, rather
it is told to point to a point halfway between each.
We then divide all of this by 180 and multiply it by π (PI)
to convert it from radians to degrees. Then the rotation
of the gun is set in terms of the variable 'angle'.
We set what happens when the mouse is pressed by using
the 'onMouseDown' function. Firstly, we create a MC variable
named 'bullet' and attach a movie to it with the linkage
name of 'bullet' (which we set earlier), a new name of
'b' and then whatever the variable 'i' is. For example,
if we pressed the mouse on the 183rd frame, the bullet
created would be called 'b183' and it would have a depth
of 183.
The next 3 lines set the x position, y position and the
rotation of the bullet to match the gun. With each bullet,
we create an 'onEnterFrame' function so we can control
its movement from the same frame as everything else. To
control its movement, we firstly set the 'x' and 'y' variables
to the sine and cosine values of the rotation in radians
of the bullet, and multiply it by 5 as our speed (make
it larger if you want it to go faster). Basically, these
variables tell the bullet to move the way that it is facing.
The next 2 lines make it so that the x and y positions
of the bullet will add on whatever the respective 'x' and
'y' variables were.
The last 'if' loop checks to see if the bullet is off
the stage or cannot be seen. If it is, we remove it so
that the code doesn't have to run the 'onEnterFrame' function
for the bullet anymore. We do this to avoid it lagging.
When you've finished, test your movie and see if it works.
You can also make it so that the gun points to something
else, like a MC or a given point. You can also change the
speed of the bullet, or customize other variables to suit
what you want. |