Groups | Blog | Home
all groups > flash actionscript > august 2006 >

flash actionscript : creating an array of movieclips in AS2 oop


new2oop
8/30/2006 10:36:37 PM
Long time proceedural programmer trying to switch to OOP. Using the book
"Object Oriented Actionscript for Flash 8" I've created the example of a class
with a movieclip passed to it as a parameter. Trying to expand that to an array
of clips/classes and without success.

Here's the class:

-----------------
class Location extends MovieClip {

var targetMC:MovieClip;
var x:Number;
var y:Number;

function Location(targetMC:MovieClip, x:Number, y:Number) {
this.targetMC = targetMC;
this.x = x;
this.y = y;
moveTo(x,y);
}

function moveTo(x:Number,y:Number) {
this.targetMC._x = x;
this.targetMC._y = y;
}

}
---------------------------
and for a single clip the following works well in the fla

//----------------------------------------------------------
attachMovie("dot","dot",1,1,100);
var myLocation:Location = new Location(dot, 200,300);
//-------------------------------------------

However when I try to create an array of 'dots' it doesn't work.

//-----------------------------------------
var myLocation:Array = new Array();
for(x=0;x<5;x++) {
attachMovie("dot","dot"+x,1,1,100+x);
var myLocation[x]:Location = new Location(_root["dot"+x], 200,100+x*30);
}
//---------------------------------------

Also how do I avoid using the _root command in this?
myIP
8/31/2006 1:42:38 AM
Below is the code should help. You were declaring myLocation array twice by
using ?var?, which is syntactically incorrect. ?_root? is referring to the
maintime. Since your code is on the maintimeline and not in an eventHandler
you can use ?this? to target it.

var myLocation:Array = new Array();
for(x=0;x<5;x++) {
trace(x)
this.attachMovie("dot","dot"+x, this.getNextHighestDepth());
myLocation[x] = new Location(this["dot"+x], 200,100+x*50);
}
new2oop
8/31/2006 1:53:42 AM
AddThis Social Bookmark Button