Chapter 11 - ManyBullets.swf

This example introduces the concept of "inheritance". A BaseObject class is defined to have a set of methods and properties that will be common to many object types. Then a Bullet class is defined to "extend" the BaseObject class. By extending the BaseObject class, instances of the Bullet class inherit the behavior of the BaseObject class. Additional Bullet-specific behaviors can then be defined.

This example also makes use of the previously defined ObjectManager, FrameTimeManager, and Timer classes.

The complete example is available in the Downloads Center. The ActionScript code for this example is:

Main Timeline code:

import ch11.ObjectManager;
import ch11.BaseObject;
import ch11.Bullet;
import ch09.FrameTimeManager;
import ch09.Timer;

ObjectManager.setTarget(this);

var tempBullet:Bullet;
var fireTimer:Timer = new Timer();

onEnterFrame = function () {
	
	FrameTimeManager.calculateFrameTime();
	
	if (fireTimer.milliseconds() > 33) {
		var randomAngle:Number = Math.floor(Math.random() * 31);
		tempBullet =
			ObjectManager.createBullet(300, 200, 400, randomAngle);
		ObjectManager.addObject(tempBullet, "collision");
		fireTimer.restartTimer();
	}
	
	var thisArray = ObjectManager.arrays["collision"];
	
	for (var i=0; i < thisArray.length; i++) {
		
		var tempObject:BaseObject = BaseObject(thisArray[i]);
		
		if (tempObject != null) {
			if (tempObject.alive) {
				tempObject.move();
			} else {
				tempObject.destroy();
				tempObject = null;
				ObjectManager.removeObject("collision", i);
			}
		}
	}
}

ActionScript 2 class code - BaseObject:

class ch11.BaseObject {
	
	var coords:Object;
	var mc:MovieClip;
	var name:String;
	var speed:Number;
	var velocity:Object;
	var angleIndex:Number;
	var alive:Boolean;
	
	public function BaseObject(x:Number, y:Number,
										movie_clip:MovieClip, name:String) {
		coords = new Object();
		velocity = new Object();
		
		coords.x = x;
		coords.y = y;
		mc = movie_clip;
		this.name = name;
		velocity.x = 0;
		velocity.y = 0;
		angleIndex = 1;
		alive = true;
		
		mc.hit_target._visible = false;
		mc.owner = this;
	}

	public function draw():Void {
		//to be overridden by subclasses
	}
	
	public function drawBounds():Void {
		//to be overridden by subclasses
	}
	
	public function move():Void {
		//to be overridden by subclasses
		mc._x = coords.x;
		mc._y = coords.y;
	}
	
	public function moveTo(x:Number, y:Number):Void {
		
		coords.x = x;
		coords.y = y;
	}

	public function destroy():Void {
		mc.removeMovieClip();
		delete this;
	}
}

ActionScript 2 class code - Bullet:

import ch11.BaseObject;
import ch09.Timer;
import ch09.MathTables;
import ch09.FrameTimeManager;

class ch11.Bullet extends BaseObject {
	
	var durationTimer:Timer;
	
	public function Bullet(	x:Number, y:Number,
									bullet_speed:Number, angle_index:Number,
									movie_clip:MovieClip, name:String) {
								
		super(x, y, movie_clip, name);
		
		angleIndex = angle_index;
		speed = bullet_speed;
		
		mc.gotoAndStop(angleIndex);
		
		durationTimer = new Timer();
		durationTimer.restartTimer();
	}

	public function move():Void {
		
		MathTables.setVelocity(angleIndex, speed, velocity);
		
		coords.x += velocity.x * FrameTimeManager.getFrameSeconds();
		coords.y += velocity.y * FrameTimeManager.getFrameSeconds();
		
		mc._x = coords.x;
		mc._y = coords.y;
		
		if (durationTimer.milliseconds() > 750) {
			alive = false;
		}
	}
}