Here's a simple sample of how to do it. The sample consists of three files. BulletClass.as which is the code for a bullet itself. BulletManager.as with which you add and update bullets. Test.fla which creates a BulletManager and adds some bullets.
This is just a sample that shows the basic logic. In the game you would have a loop in which you call BulletManager.Update(). In the game you would add code in the BulletManager which removes nulled bullets from the array. And of course the BulletClass would contain some graphical elements. :)
BulletClass.as:
package {
import BulletManager;
public class BulletClass {
private var Distance:Number = 0;
public function UpdateBullet():void {
Distance += 1;
trace("A bullet has flown for distance of",Distance);
if (Distance >= 3) {
trace("A bullet has flown far enough.");
BulletManager.Remove(this);
}
}
}
}
BulletManager.as:
package {
import BulletClass;
public class BulletManager {
public static var Bullets:Array = new Array();
public static function Add():void {
trace("BulletManager adds a new bullet.");
Bullets.push(new BulletClass());
}
public static function Remove(pC):void {
trace("BulletManager needs to remove a bullet...");
for (var i:int = 0; i < Bullets.length; i++) {
if (Bullets[i] != null) {
if (Bullets[i] == pC) {
Bullets[i] = null;
trace("...and removed it!");
}
}
}
}
public static function Update():void {
trace("BulletManager starts updating bullets...");
for (var i:int = 0; i < Bullets.length; i++) {
if (Bullets[i] != null) {
Bullets[i].UpdateBullet();
}
}
trace("BulletManager ends updating bullets.");
}
}
}
Code in Test.fla:
import BulletManager;
BulletManager.Add(); // fires a bullet
BulletManager.Update(); // updates all bullets
BulletManager.Add(); // fires another bullet
BulletManager.Add(); // fires yet another bullet
BulletManager.Update(); // updates all
BulletManager.Update();
BulletManager.Update();
BulletManager.Update();
When running Test.fla, the output should be:
BulletManager adds a new bullet.
BulletManager starts updating bullets...
A bullet has flown for distance of 1
BulletManager ends updating bullets.
BulletManager adds a new bullet.
BulletManager adds a new bullet.
BulletManager starts updating bullets...
A bullet has flown for distance of 2
A bullet has flown for distance of 1
A bullet has flown for distance of 1
BulletManager ends updating bullets.
BulletManager starts updating bullets...
A bullet has flown for distance of 3
A bullet has flown far enough.
BulletManager needs to remove a bullet...
...and removed it!
A bullet has flown for distance of 2
A bullet has flown for distance of 2
BulletManager ends updating bullets.
BulletManager starts updating bullets...
A bullet has flown for distance of 3
A bullet has flown far enough.
BulletManager needs to remove a bullet...
...and removed it!
A bullet has flown for distance of 3
A bullet has flown far enough.
BulletManager needs to remove a bullet...
...and removed it!
BulletManager ends updating bullets.
BulletManager starts updating bullets...
BulletManager ends updating bullets.