Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Advanced Weapon System


Made additional changes and improvements based on feedback from the community.

Change List v1.1.2

  • Sabdrenss128 helped optimize the remove object method
  • Added constants for weapon types
  • Optimized bullet/laser code
  • Removed laser memory leak bug
  • Other speed optimizations

Preview v1.1.2

Download Fla Sample

Download Fla Sample

Weapon Manager Methods

  • Add Weapon
  • Remove Weapon
  • Fire Weapons
  • Stop Weapons
  • Add Bullet
  • Remove Bullet
  • Add Solid Object
  • Remove Solid Object
  • Update Weapons
  • Update Bullets
  • Check Collisions

Weapon Methods

  • Constructor
  • Create Barrel
  • Fire Barrels
  • Update Rotation
  • Update Barrels
  • Create Display objects
  • Destroy Display objects
  • Destroy

Available Weapon Settings

  • ID
  • Skin
  • Current x, y, rotation
  • Max rotation speed
  • Reload speed
  • Bullet spread (accuracy)

Available Barrel Settings

  • Skin
  • Barrel Length
  • Current x, y, rotation

Available Bullet Settings

  • Skin
  • Bullet speed
  • Max travel distance
  • Bullet life time
  • Current x, y, rotation

Change List v1.1.1

  • Added Laser weapon
  • Gave bullets life timer
  • Removed unused vars
  • Added global shared vars
  • Updated package names

Refactored my AS3 multi-barrel gun code to be even more flexible using the Composition design pattern. This allowed me to create a weapon system that’s easily extendable to include different types of weapons.

Current implementation includes single, dual and quad barrel projectile guns and laser guns. Included weapon classes are easily duplicated to create additional different weapons. Laser guns can be set to pulse or beam settings. See examples for details.

I also added a WeaponManager class that handles creation, update and destroy functions of weapons, bullets and solid objects. Performs collision detection, display updates and more. Easily add your enemies thru the weapon manager and let it handle all the collision detection/reaction.

Added custom skin support for the Weapon platform, Barrel/Turret, Bullet & Laser beam. Laser beam is easily customizable as well.

Wish List

  • Give each bullet its own target
  • Fire one barrel at a time
  • Fire multiple bullets from one barrel
  • Add Guided Missile weapon
  • Better collision detection
  • Use interfaces
  • Add DisplayManager (perhaps use Blitting to draw graphics)
  • Use Vectors instead of Arrays

13 Responses

Wow! Thank you very much! Nice weapon system. Just one suggestion though: how about one simple dual cannon for demonstrating purposes only? Keep up the great work!:)

  • Hi drpelz,

    Glad you like the example.

    For a single weapon example, open and comment out the other weapon examples. Open to see how to setup the weapon.


  • Hi. I know this is a bit late but a few suggestions:
    1) Array is way slower than vector. If you are having an array of objects of similar class use Vector. Vector is almost 2 times faster. It helps a little, but still does.
    2) Why do you need an ID for an object that is basically a sprite? Sprites have names, which are basically it. Additionally, you can get children by name and do some other awesome stuff(probably, dunno).

  • im was playing around with this (in code) and i saw that trying to make duplicate turrets is impossible/hard? i AM a begginer though, so could u tell me how if its possible?

  • I remembered this site from ages ago and thought I’d stop by to have a look. Turns out we’ve been playing with some quite similar things in the meantime.

    One thing I noticed here that I also had happen is that if you look at the projectiles (easiest to see on the quad cannon) you can see that they don’t always move at a constant speed. This is happening because when a projectile is removed from the display list the splice causes the next projectile in the array to not be updated that frame. The solution is a simple one, just decrement the current index by one every time you remove something (and update the length if thats been cached as well).

    Simple Example: Only 2 projectiles
    Currently on projectile 0; i=0
    projectile 0 should be removed
    projectile 0 is spliced from array, projectile 1 becomes projectile 0
    loop terminates as now i=1 and the length of the array is 1, yet the (new) projectile 0 was not told to update.

    • So that’s what it was! I couldn’t see it for the life of me. Haha! Thank you, rich!


      • No problem. This was actually one of the first places I found when I was starting programming, and it really helped me, so its nice to be able to give something back.

        Soon after I fixed the array splicing issue I moved to using a linked list. The performance benefits are immense when you want to remove a lot of things from a large list.

  • love the system! buttt

    i would like to make a gun follow a car, i got it to work bus the bullets alsow followed the car. i think this is bevause of the container where the gun and bulletes are added.

    how could i get the bullets to go thair own way?

  • Leave a Reply

    You must be logged in to post a comment.