Keeping track of dynamic MCs

so i’ve got a question…

What is the best way to keep track of and be able to call on dynamically loaded MCs that are attached from the library?

ok… basically i’ve got this xml document that loads (just fine) into flash and attaches MCs based on the child nodes. What i want to do is be able to call one function and have it repeat on all the MCs taht are attached on the stage.

Any suggestions? So far I’ve thought about using an array to keep up with them, but I haven’t tried that. So far I’ve only got loops that aren’t working.

Thanks in advance,

michael // bodyvisual

I am making a game that does that exact thing, and an array loop is the only real way to keep effective track of them all.

so how do you sort through the MCs on the stage? do you make the array when you load and attach the MCs?

thx

:p:

bump

:p:

There’s a few ways.

First of all, you can reference dynamic movie clips like this:

location[“name”+var]

example: _root[“mymovie”+i]

if you use a for() loop to go through the ‘i’ variable, you can affect all the movie clips in the _root timeline named mymovie1, mymovie2, mymovie3, etc.

example:
for(i=1; i<5; i++){
_root[“mymovie”+i]._x += 20;
}

Another way is to use for…in syntax, which will loop through an object and find everything contained within.

example:

for(variablename in object){
trace(variablename);
}

variablename is a variable of your own choosing, object is the movieclip, location, array, etc. to loop through.

for(j in _root){
trace(j);
}

You can do a lot of stuff with both of these, but I’ll leave something to your imagination. :slight_smile:

Good luck!

–EP

thanks a lot man! will try these out right now and see what i get.

thanks a lot, and i mean that :smiley:

:p:

ok… well, i have gotten the code working, but it only effects the last symbol for some reason (i’m calling the same function in a loop). I wonder why it’s only affecting one of them?

thx for the help.

:p:

bump i still can’t get the one function to affect all of the MCs… any more suggestions anyone?

thx.

:p:

post the code…

ok… haha, i should thought of this earlier… :slight_smile:

work = new XML();
work.ignoreWhite = true;
work.onLoad = function(good) {
 if (good) {
  total = this.firstChild.childNodes;
  count = 1;
  control = new Array();
  onEnterFrame = function () {
   if (count<total.length) {
	count++;
	clicks = _root.attachMovie("button", "button"+count, count);
	controlled = control.push("button"+clicks);  //finally got the array working
	//trace(controlled);
	//trace(control);
	randX = Math.round(Math.random()*750);
	randY = Math.round(Math.random()*550);
	clicks._x += randX;
	clicks._y += randY;
	clicks.hover = total[count].firstChild;
	clicks.loader = total[count].attributes.href;
	[color=red]clicks.onRelease = function() {
	 //trace(controlled);
	 speed = 10;
	 newX = 0;
	 newY = 0;
	 onEnterFrame = function() {
	  distX = (newX-this._x)/speed;
	  distY = (newY-this._y)/speed;
	  this._x += distX;
	  this._y += distY;
	 }
	};[/color]
	clicks.onRollOver = function() {
	 hover.hoverTxt.text = this.hover;
	};
	clicks.onRollOut = function() {
	 hover.hoverTxt.text = "";
	};
   }
  };
 }
};
work.load("work.xml");

Thx for looking… the part in red is what’s the trouble…

:p:

It seems that you are resetting ‘clicks’ every time you run through the loop because ‘clicks’ is not a unique instance name, even though it is referencing a unique object.
Instead of using clicks just use this[“button”+count]
That should work I think.

–EP

yes, but that would only control the one MC. I want to be able to run a function on all of the buttons…

thx :slight_smile:

yeah, that’s when you have to use an array and loop through it. You might also be able to do it by setting up child / parent relationships, but I always use the array.

–EP

hmmm… i shall try it and post code. thanks for your help m8

:p:

ok… i’m having NO luck whatsoever in this… can you write out what you meant? thx

:p:

Flashkit has some nice forums too.

well, explain what you want. Do you want when you click on one object to have that function fire for all objects, or just that specific one?

–EP

I want the same animation function to be triggered on all the MCs on the stage. thx

:p:

yeah, for that, you should loop through the movie and execute them manually. if you are consistent with naming your movie clips, you can do it like this:

for(i=0; i<=20; i++){
this[“myobject”+i].onPress=function(){
for(k=0; k<=20; k++){
this[“myobject”+k]._x +=20;
}
}
}

that’ll cause all the instances of myobject that you create (or the first 21 of them anyways) to all move to the right by 20 pixels.

I had a long day at work or I’d help more.

good luck man!

–EP

that’s brilliant! i will try that as soon as i get to work. thanks super for your help!

:p: