PDA

View Full Version : [AS3] Gun shooting a bullet

Drezer
02-05-2009, 03:56 AM
I am wanting to try to create a simple shooting game where you control a gun with your arrow keys and push another button, lets says "control" to fire a bullet. what would i do to make the bullet play the movie clip once and in specific x,y cords to make the bullet MC start at the tip of the gun/barrel.

SurgicalSteel
02-05-2009, 04:20 AM
For playing the bullet movie clip once, you can throw a bulletMC.play() into the code that runs when you hit the control key.

To position the bullet at the tip of the gun
If you know the length of the barrel, where it is, and what direction it's facing you can do it as follows:
I'm am going to assume that the registration point for the gun is at the back of the barrel, and the barrel's default position is verticle. so the tip of the barrel is above the reported location of the barrel.

Figure out the height of the gunMC
Figure out which direction the gun is facing
Use the point class to construct a vector pointing the same direction as the gun (what we found in step 2) with the magnitude of the barrel length (what we found in step 1). Like this: Point.polar(length, angle) where angle is in radians
Make a Point out of the location of the gun
Add the vector we got from step 3 to the vector from step 4
Use the result's x and y properties to position bulletMC

There are other ways to do it using trig instead of vectors, but I like that way since vectors lend themselves well to shooting.

Drezer
02-05-2009, 01:25 PM
I'll load you my fla file and maybe you can take a look at it and see if it
can be done the way you mentioned.

02-05-2009, 06:51 PM
The zip you attached is corrupt (or so it says). Here are some fla examples on what you want to do:

Player movement:
http://www.freeactionscript.com/2009/02/multiple-key-press-detection

Gun:
http://www.freeactionscript.com/2008/10/game-engine-shooting-projectile-bullets/

:)

Drezer
02-05-2009, 09:32 PM
The zip you attached is corrupt (or so it says). Here are some fla examples on what you want to do:

Player movement:
http://www.freeactionscript.com/2009/02/multiple-key-press-detection

Gun:
http://www.freeactionscript.com/2008/10/game-engine-shooting-projectile-bullets/

:)

thanks man, that gun one helped me with the placement of the bullet. what i really need now is to get duplicateMovieClip in there so it doesnt just replace the bullet when i fire a new one, and i need to put in a speed.