This is version 3 of my Flash game projectile weapons script. It is totally rewritten & optimized to support multiple weapons, multiple enemies & collision detection. All the functions are encapsulated for easy re-use.
Features:
Weapon
- Number of bullets
- Bullet speed
- Bullet age
- Reload speed
Weapon Management
- Weapon Database
- Weapon Database Search
- Equip Weapon Function
- Reload Weapon
- Create Bullet
- Update Bullet
- Destroy Bullet
Enemy Management
- Create enemy
- Enemy Collision Detection with Bullets
- Destroy Enemy
Update
AS3 version of this script is available here:
http://www.freeactionscript.com/2011/07/projectile-weapon/

If you wish to have the bullet deleted when hitting the enemy, find “function checkEnemyHit” and under where it says “destroyEnemy(tempEnemy);” put this:
destroyBullet(_bullet);thanks this is really good stuff you got there
but i need help with something in the rotation part of it. this is the code i got so far
var radians2:Number = 180/Math.PI;var maxAng:Number = 89;
var rSpeed:Number =10;
var playerX = player_mc._x;
var playerY = player_mc._y
//Rotate Player
function rotatePlayer()
{
//calculate player_mc rotation, based on player position & mouse position
player_mc._rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
//player_mc._rotation = rotationDirection;
if (player_mc._rotation > maxAng)
{
player_mc._rotation = maxAng;
}
else if (player_mc._rotation < -maxAng)
{
player_mc._rotation = -maxAng;
updateAfterEvent();
}
}
now i need help with adding a lil more natural feel to it. i want to be able to add a rotation speed to decide how far the hero follows the mouse. hope i make sence
mixedtrigeno, a script that does just that is in the works and is almost ready to be released.
I will try to clean it up and release as soon as I can.
Thanks
oh great thanks ill keep checking out for it. i was trying to figure out why the rotation code in v3 doesnt work if the figure is not a circle…
I tried adding VERY SIMPLE movement to the script, yet it stops the rotational movement? Any ideas? I am a noob to this sorry. Just trying to learn:
this.onEnterFrame = function(){
if (Key.isDown(Key.RIGHT))
{
player_mc._x += 10;
}
else if (Key.isDown(Key.LEFT))
{
player_mc._x -= 10;
}
else if (Key.isDown(Key.UP))
{
player_mc._y -= 10;
}
else if (Key.isDown(Key.DOWN))
{
player_mc._y += 10;
}
}
I am thinking it might have something to do with the very first line?
I love this site!! Spent absolutely ages looking around for a good site for some cool things for a game I am making and this finally comes along. xD
Thanks Zorbia! Glad you like it
-Phil
I tried to get the enemies to move (which works), and have them move after they respawn (which doesn’t work). I combined the code from this script with the enemy following code. Please help, my grade in programming depends on it.
Hi Umbjabaya, please post a link to your fla file. Maybe someone will be able to help…
-Phil
i tried to make the enemies move with the the multiple enemies follow script but it wont work, can anyone help?
I’m making a little RPG game and everything is going fine. However, how could I make bullets delete when they collide with certain movieclips, such as a wall or other scenery.
Also, when I move to a new frame in the main timeline (for a new level), the bullets fired previously do not delete automatically after 2 seconds. How can I make a function that clears all bullets from the stage or clears the bullet array?
Thanks for any help = D
Dear W4ffleKing,
There is a function called destroyBullet that deletes bullets. Just pass the bullet you want deleted as an argument:
destroyBullet(bulletYouWantDeleted);I do not advise using more than 1 frame on the timeline as it creates all sorts of problems (as you can see). Instead, try placing all your levels in a container clip.
Thank you very much for the quick reply = D
One last thing I would like to know… I see you have hitTests for enemies that you attach using actionscript. However, I can’t work out how I can do a hitTest on bullets with movieclips already on the stage. Thanx agen!
Ah, never mind. I worked out how to do it:
function collisionBullets():Void{
for(i = 0; i < bulletArray.length; i++)
{
if(_root.car.hitTest(bulletArray[i]))
{
//remove Movie Clip from stage
removeMovieClip(bulletArray[i]);
//remove bullet from array
bulletArray.splice(i,1);
//clear timers
clearInterval(tempBullet.lifeTimer);
}
}
}
Then add collisionBullets() to the enterFrame function.
Hi W4ffleKing,
I would put all those movieclips you have on stage in an array and run a for loop on that array hittesting all the items.
Same concept as running a for loop to hittest bullets with enemies.
Ok, will do – thanks again for the help. ^^
sorry for this noob question but once i download the “source” and i open it with cs5 it only shows me the screen with the player and the background. where is the code and where can i find it? ive been looking for about a half hour know and i cant seem to find it can someone please help me?
Hi noobatcs5,
Look on the timeline, frame 1.
-PR
I don`t get why the destroyBullet code ain`t written like this:
destroyBullet(bullet);
Instead of this:
destroyBullet(_bullet);
I mean in the code we call the bullet for “bullet” and not “_bullet”.
Can anybody please tell me where the bullet gets its name in the code. I know it maybe sounds like a stupid question, but it helps me much in understanding the code better!!!
Thanks
GTA,
The function takes an argument – aka we are passing a variable to it. We need to give that variable a unique name. I named it “_bullet”. You can name it whatever you want.
function destroyBullet(_bullet:MovieClip):Void{
// to access the passed variable
// we call it by it's argument name "_bullet"
}
Does that make sense?
I ant it to be so that when the player shoots his gun(whether is an automatic, pistol, or shotgun), the bullet comes out of the barrel. The bullet instead comes out of the center of the player. I’ve tried making a barrel movieclip and replacing player_mc with barrel_mc where it says to position bullet on player but the bullet doesn’t even appear. Can someone help me on this?
Hi Curlyfriez,
Each weapon has a variable called “barrelLength”. Change it to how far you want the bullet to be created from the player.
-PR
I’m wondering why there is a lifetime on these bullets… Wouldn’t it be sufficient enough to destroy them as soon as they hit the edge of the screen? (and how would you do that? I’m breaking my head on that one… Just can’t figure out some of the code
)
Hi dammagamer,
What if your map is bigger than the stage? You want the bullets to hit stuff that’s just off stage.
If you want to destroy bullets that hit the edge, just add an if statement to the updateBullets function that checks if the bullet is outside of stage.
Something like this:
if(temporaryBullet._x < 0 || temporaryBullet._x > 500 || temporaryBullet._y < 0 || temporaryBullet._y > 500)
{
destroyBullet(temporaryBullet);
}
I have another problem that might be easy to solve but i can’t see it.
In the actionscript for uploading the bullet into the game, it says:
var tempBullet:MovieClip = _root.attachMovie(“bullet”, “b”+_root.getNextHighestDepth(), _root.getNextHighestDepth());.
I have tried to apply some of this script into the same game idea, but i can’t get the player to shoot. Nothing happens when he shoots. does it have something to do with the “b”? Because i don’t know where to put it in the bullet movieclip or somewhere else. Sorry about this, but can some help me on this?
Hi Curlyfriez,
Try this http://goo.gl/RGz2A
i just downloaded the files but no code? only the .fla
can i get the code plz
datawrangler,
The code is on the first frame of the timeline.
-PR
Can someone please convert this to AS3 for me
Hello pradvan, love your work especially the fact your doing it for free
, ive just ran across one small problem unfortunately
ive been using your enemy following ai and ive successfully integrated it with my weapon shooting, but unfortunately i kidna messed up in it and i cant customize some crucial things i need in it haha
, so i came across this file just now and im wondering how i would go about adding different weapons with different bullets since this system uses one bullet mc, and also making it so when you switch weapons while one of the bullet is flying the bullet itself doesn’t switch.
Thanks for your help in advance, and love your work
Also want to add that my games a basic zombie survival, zombies spawn onto the stage and well its endless XD, i still haven’t gotten to figuring how to make the enemy’s themselves spawn…
Hi Mic,
Unfortunately I no longer provide AS2 support. Please seek help wit AS2 questions on one of the forums listed on the site.
Thank you,
Philip Ravan