Chapter 7 - SimpleSquare2.swf

This example further demonstrates the use of ActionScript 2 class files. The code in the "SimpleSquare2.as" class file is "imported" into the FLA. Similarly, the code in the "GameDepthManager.as" file is imported into the the "SimpleSquare2.as" file, allowing the SimpleSquare2 class to make use of the methods and properties of the GameDepthManager class.

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

Main Timeline code:

import ch7.SimpleSquare2;

var size = 20;

for (i=0; i<5; i++) {
	for (j=0; j<5; j++) {
		
		var x_coord = i * (size + 5);
		var y_coord = j * (size + 5);
		var tempSquare:SimpleSquare2 = new SimpleSquare2(x_coord, y_coord, size, this);
		tempSquare.draw();
	}
}

ActionScript 2 class code - SimpleSquare2:

import ch7.GameDepthManager;

class ch7.SimpleSquare2 {

	// Properties
	public var coords:Object;
	public var name:String;
	public var size:Number;
	public var mc:MovieClip;
	public var movieClipTarget:MovieClip;

	// Constructor Method
	public function SimpleSquare2(	x:Number, y:Number, size:Number,
									target:MovieClip) {
		coords = new Object();	
		coords.x = x;
		coords.y = y;
		this.size = size;
		movieClipTarget = target;
		
		var depth:Number = GameDepthManager.getNextObjectDepth();
		name = "mySquare_" + depth;
		mc = movieClipTarget.createEmptyMovieClip(name, depth);
		mc._x = coords.x;
		mc._y = coords.y;
	}

	//draw method
	public function draw():Void {
		mc.beginFill( 0x0022CC, 80 );
		mc.lineStyle( 2, 0xFF9900, 100 );
		mc.moveTo( 0, 0 );
		mc.lineTo( size, 0 );
		mc.lineTo( size, size );
		mc.lineTo( 0, size );
		mc.lineTo( 0, 0 );
		mc.endFill();
	}
}

ActionScript 2 class code - GameDepthManager:

class ch7.GameDepthManager{
	
	static var MIN_OBJECT_DEPTH:Number = 10;
	static var MAX_OBJECT_DEPTH:Number = 399;
	static var SHIP_DEPTH:Number = 450;
	static var HUD_DEPTH:Number = 500;
	static var MIN_AUDIO_CLIP_DEPTH:Number = 1000;
	static var MAX_AUDIO_CLIP_DEPTH:Number = 1200;
	
	static var nextObjectDepth:Number = MIN_OBJECT_DEPTH;
	static var nextAudioClipDepth:Number = MIN_AUDIO_CLIP_DEPTH;
	
	public static function getNextAudioClipDepth():Number {
		
		// recycle depths
		if (nextAudioClipDepth >= MAX_AUDIO_CLIP_DEPTH)
			nextAudioClipDepth = MIN_AUDIO_CLIP_DEPTH;
		return nextAudioClipDepth++;
	}
	
	public static function getNextObjectDepth():Number {
		
		// recycle depths
		if (nextObjectDepth >= MAX_OBJECT_DEPTH)
			nextObjectDepth = MIN_OBJECT_DEPTH;
		return nextObjectDepth++;
	}
}