Chapter 10 - ArrayOfDiamondsOM.swf
In this example, Arrays are used to keep track of the application's many objects - in this case, Diamonds.
This example introduces a generally useful object management technique. An ObjectManager class is used as a "factory" for creating all of the application's object instances. Centralizing this functionality can make complex applications much easier to maintain.
The Diamond class is an example of an object type that can be created by the ObjectManager factory. In this example, the Diamond instances have an undulating, wave-like motion controlled by their draw() method. Clicking on the diamonds causes them to be destroyed. Clicking on the button in the lower left causes the Array to be reversed.
The techniques presented in this example can be used for a wide range of features, including dynamic navigation, buttons, etc.
The complete example is available in the Downloads Center. The ActionScript code for this example is:
Main Timeline code:
import ch10.ObjectManager;
import ch10.Diamond;
ObjectManager.setTarget(this);
var diamonds:Array = new Array();
var newDiamond:Diamond;
var diamondCount = 10;
var sortAction = null;
var name:String;
var depth:Number;
for (var i=0; i<diamondCount; i++) {
myDiamond = ObjectManager.createDiamond(20 + (i * 55),70,50);
myDiamond.draw();
ObjectManager.addObject(myDiamond, "diamonds");
}
trace("The diamonds Array contains:");
trace(ObjectManager.arrays["diamonds"].length + " elements:\n");
trace(ObjectManager.arrays["diamonds"].toString());
name = "array_length_txt";
depth = this.getNextHighestDepth();
this.createTextField(name, depth, 5, 5, 100, 22);
onEnterFrame = function () {
var thisArray = ObjectManager.arrays["diamonds"];
if (sortAction == "reverse") {
thisArray.reverse();
for (var i=0; i<thisArray.length; i++) {
thisArray[i].moveTo(20 + (i * 55), 70);
}
sortAction = null;
}
for (var i=0; i < thisArray.length; i++) {
var thisDiamond:Diamond = Diamond(thisArray[i]);
if (thisDiamond != null) {
if (thisDiamond.alive) {
thisDiamond.move();
} else {
thisDiamond.destroy();
thisDiamond = null;
ObjectManager.removeObject("diamonds", i);
}
}
}
array_length_txt.text = "Length: " + thisArray.length;
}
sort_control.onRelease = function () {
sortAction = "reverse";
}
name = "ctrl_txt";
depth = this.getNextHighestDepth();
this.createTextField("ctrl_txt", depth, 0, 0, 100, 22);
ctrl_txt._x = sort_control._x + 20;
ctrl_txt._y = sort_control._y - 10;
ctrl_txt.text = "Reverse the Array";
ActionScript 2 class code - ObjectManager:
import ch7.GameDepthManager;
import ch10.Diamond;
class ch10.ObjectManager {
static var movieClipTarget:MovieClip;
static var arrays:Object = new Object();
public static function setTarget(target:MovieClip) {
movieClipTarget = target;
}
public static function createDiamond(x:Number, y:Number,
size:Number):Diamond {
var depth = GameDepthManager.getNextObjectDepth();
var name = "d_" + depth;
var emptyMovieClip:MovieClip =
movieClipTarget.createEmptyMovieClip(name, depth);
var newDiamond:Diamond =
new Diamond(x, y, size, emptyMovieClip, name);
return newDiamond;
}
public static function addObject(object:Object, array_id:String) {
if (arrays[array_id] == undefined) {
arrays[array_id] = new Array();
}
arrays[array_id].push(object);
}
public static function removeObject(array_id:String, index:Number) {
arrays[array_id].splice(index, 1);
}
}
ActionScript 2 class code - Diamond:
class ch10.Diamond {
var coords:Object;
var drift:Object;
var mc:MovieClip;
var name:String;
var size:Number;
var top:Object;
var right:Object;
var bottom:Object;
var left:Object;
var textField:TextField;
var alive:Boolean;
public function Diamond(x:Number, y:Number, size:Number,
movie_clip:MovieClip, name:String) {
coords = new Object();
drift = new Object();
coords.x = x;
coords.y = y;
this.size = size;
mc = movie_clip;
mc.owner = this;
this.name = name;
drift.x = 0;
drift.y = 0;
alive = true;
top = {x:size/2, y:0};
right = {x:size, y:size/2};
bottom = {x:size/2, y:size};
left = {x:0, y:size/2};
mc.createTextField(name, 10, 0, 0, 100, 20);
textField = mc[name];
textField.text = name;
mc.onRelease = function() {
trace(this.owner.name);
this.owner.alive = false;
}
}
public function move():Void {
var period:Number = 2000;
var phase:Number = getTimer() % period;
var frequency:Number = 1;
var angle:Number = phase/period * frequency * Math.PI*2;
var movieWidth:Number = 600;
var phaseOffest:Number = (coords.x / movieWidth) * Math.PI*2;
var amplitude:Number = 25;
var yOffset = Math.sin(angle + phaseOffest) * amplitude;
mc._x = coords.x + drift.x;
mc._y = coords.y + yOffset + drift.y;
drift.x *= .9;
drift.y *= .9;
}
public function moveTo(x:Number, y:Number):Void {
drift.x = coords.x - x;
drift.y = coords.y - y;
coords.x = x;
coords.y = y;
}
public function draw():Void {
mc.clear();
mc.colors = [0xFF0000, 0x0000FF];
mc.alphas = [100, 100];
mc.ratios = [0, 0xFF];
mc.matrix = {matrixType:"box", x:12.5, y:12.5,
w:25, h:25, r:(45/180)*Math.PI};
mc.beginFill( 0x0022CC, 80 );
mc.lineStyle( 2, 0xFF9900, 100 );
mc.moveTo( top.x, top.y );
mc.lineTo( right.x, right.y );
mc.lineTo( bottom.x, bottom.y );
mc.lineTo( left.x, left.y );
mc.lineTo( top.x, top.y);
mc.endFill();
}
public function destroy():Void {
mc.removeMovieClip();
delete this;
}
}

