View Full Version : How to refer to multiple MC instances with 1 line
maximum rinse
10-12-2006, 02:43 PM
Throughout my game, I'm attaching a bunch of movieClips, and then applying varying (or even the same) properties to each individual movieClip.
An example of what I'm doing below:
for (var j:Number = 0; j<Rcounter; j++)
{
var crate:MovieClip = this.attachMovie("crate", "crate"+j, j);
}
StackHeight = 330;
//var crateJ:MovieClip = this["crate" + j];
//crateJ._height = StackHeight/5;
crate0._height = StackHeight/5;
crate1._height = StackHeight/5;
crate2._height = StackHeight/5;
crate3._height = StackHeight/5;
crate4._height = StackHeight/5;
crate5._height = StackHeight/5;
crate6._height = StackHeight/5;
crate7._height = StackHeight/5;
crate8._height = StackHeight/5;
crate9._height = StackHeight/5;
The problem is really that I'm ending up with rediculously long code, and I'm repeating stuff again and again for each instance.
Surely there is a way to refer to all instances at once?
I was thinking of something like:
crate[j]._height = StackHeight/5;
or something, but that doesn't seem to work...
Any suggestions appreciated, especially good ones!
try:
stackheight=330;
for (var j:Number = 0; j<Rcounter; j++)
{
var crate:MovieClip = this.attachMovie("crate", "crate"+j, j);
crate._height=stackheight/5;
}
maximum rinse
10-12-2006, 03:29 PM
TG, thanks for the reply, but surely this will give all instances the same property.
Maybe that was a bad example.. How about this code:
for (var m:Number = 0; m<Rcounter; m++) {
var crate:MovieClip = attachMovie("tick_mc", "tick_mc"+m, m+50);
var crate:MovieClip = attachMovie("cross_mc", "cross_mc"+m, m+150);
}
tick_mc0._x = cross_mc0._x=crate0._x;
tick_mc1._x = cross_mc1._x=crate1._x;
tick_mc2._x = cross_mc2._x=crate2._x;
tick_mc3._x = cross_mc3._x=crate3._x;
tick_mc4._x = cross_mc4._x=crate4._x;
// and on and on
tick_mc0._y = cross_mc0._y=crate0._y;
tick_mc1._y = cross_mc1._y=crate1._y;
tick_mc2._y = cross_mc2._y=crate2._y;
// and on and on
cross_mc0._visible = false;
cross_mc1._visible = false;
cross_mc2._visible = false;
cross_mc3._visible = false;
cross_mc4._visible = false;
cross_mc5._visible = false;
cross_mc6._visible = false;
cross_mc7._visible = false;
cross_mc8._visible = false;
cross_mc9._visible = false;
tick_mc0._visible = false;
tick_mc1._visible = false;
tick_mc2._visible = false;
tick_mc3._visible = false;
tick_mc4._visible = false;
tick_mc5._visible = false;
tick_mc6._visible = false;
tick_mc7._visible = false;
tick_mc8._visible = false;
tick_mc9._visible = false;
if (Rtick_array[0] == true) {
tick_mc0._visible = true;
} else if (Rtick_array[0] == false) {
cross_mc0._visible = true;
}
if (Rtick_array[1] == true) {
tick_mc1._visible = true;
} else if (Rtick_array[1] == false) {
cross_mc1._visible = true;
}
if (Rtick_array[2] == true) {
tick_mc2._visible = true;
} else if (Rtick_array[2] == false) {
cross_mc2._visible = true;
}
if (Rtick_array[3] == true) {
tick_mc3._visible = true;
} else if (Rtick_array[3] == false) {
cross_mc3._visible = true;
}
if (Rtick_array[4] == true) {
tick_mc4._visible = true;
} else if (Rtick_array[4] == false) {
cross_mc4._visible = true;
}
if (Rtick_array[5] == true) {
tick_mc5._visible = true;
} else if (Rtick_array[5] == false) {
cross_mc5._visible = true;
}
if (Rtick_array[6] == true) {
tick_mc6._visible = true;
} else if (Rtick_array[6] == false) {
cross_mc6._visible = true;
}
if (Rtick_array[7] == true) {
tick_mc7._visible = true;
} else if (Rtick_array[7] == false) {
cross_mc7._visible = true;
}
if (Rtick_array[8] == true) {
tick_mc8._visible = true;
} else if (Rtick_array[8] == false) {
cross_mc8._visible = true;
}
if (Rtick_array[9] == true) {
tick_mc9._visible = true;
} else if (Rtick_array[9] == false) {
Get the point?
So what I would like to do is compress all of this into a few lines.
What I had in mind was something like this:
[CODE]
for (var m:Number = 0; m<Rcounter; m++) {
var crate:MovieClip = attachMovie("tick_mc", "tick_mc"+m, m+50);
var crate:MovieClip = attachMovie("cross_mc", "cross_mc"+m, m+150);
}
tick_mcm._x = cross_mcm._x=cratem._x;
tick_mcm._y = cross_mcm._y=cratem._y;
cross_mcm._visible = false;
if (Rtick_array[m] == true) {
tick_mcm._visible = true;
} else if (Rtick_array[m] == false) {
cross_mcm._visible = true;
}
[CODE]
So I guess to summarise, my question is:
When creating multiple movie clips, how do I give them all different properties with a couple of lines?
THanks!
first, i wouldn't do:
for (var m:Number = 0; m<Rcounter; m++) {
var crate:MovieClip = attachMovie("tick_mc", "tick_mc"+m, m+50);
var crate:MovieClip = attachMovie("cross_mc", "cross_mc"+m, m+150);
i would make it
for (var m:Number = 0; m<Rcounter; m++) {
var crate1:MovieClip = attachMovie("tick_mc", "tick_mc"+m, m+50);
var crate2:MovieClip = attachMovie("cross_mc", "cross_mc"+m, m+150);
also in your example, you have not defined a 'crate0', 'crate1','crate2' etc.
in fact, your 'crate' you create for tick_mc is over written by the second crate line for cross_mc.
so... in your for loop, you are creating 2 new movie clips. you need to give them different names. if you want to referense them later.
outside your for loop. you want to modify your created movieclips.... i see tick_mc0,1,2,3, ect. those are created in the first line of your for loop.... i see cross_mc0,1,2,3, etc, they are created in the second line of your for loop.
i dont see where you are creating the mc crate0,1,2,3 etc.
you can do all of this stuff inside the for loop where you dynamically create the mcs.
some thing like
for (var m:Number = 0; m<Rcounter; m++) {
this.attachMovie("tick_mc", "tick_mc"+m, m+50);
this.attachMovie("cross_mc", "cross_mc"+m, m+150);
this["tick_mc"+m]._x =this["cross_mc"+m]._x=???;
this["tick_mc"+m]._y =this["cross_mc"+m]._y=???;
this["tick_mc"+m]._visible =this["cross_mc"+m]._visible=false;
if (Rtick_array[m] == true) {
this["tick_mc"+m]._visible = true;
} else if (Rtick_array[m] == false) {
this["cross_mc"+m]._visible = true;
}
}
something like that.
you can still use the original syntax you have set.... with the var crate.... etc. that basically just sets up a reference to the actual object created. so you can change the properties of 'crate', and it is actually referencing 'tick_mc0' or tick_mc1 etc depeding on where you are in the loop. i modified my last piece to not use references so you can see how it is done in the loop.
maximum rinse
10-13-2006, 10:34 AM
That's great TG, thanks very much.
First of all I didn't even realise I was calling my ticks and crossed 'crate' as you pointed out. Anyway, I managed to refer to each instance using
this["newname"+counter]._property
and combined a bunch of code together, and ended up with something like this:
for (var m:Number = 0; m<Rcounter; m++) {
this.attachMovie("crate", "crate"+m, m);
this.attachMovie("tick_mc", "tick_mc"+m, m+50);
this.attachMovie("cross_mc", "cross_mc"+m, m+150);
StackHeight = 330;
this["crate"+m]._height = StackHeight/5;
this["crate"+m]._width = this["crate"+m]._height*1.85;
//y positions
this["crate"+0]._y = 400-(this["crate"+0]._height/2);
this["crate"+m]._y = this["crate"+m-1]._y-50;
//this["crate"+m]._height
crate1._y = crate0._y-crate1._height;
crate2._y = crate1._y-crate2._height;
crate3._y = crate2._y-crate3._height;
crate4._y = crate3._y-crate4._height;
crate5._y = crate0._y;
crate6._y = crate0._y-crate1._height;
crate7._y = crate1._y-crate2._height;
crate8._y = crate2._y-crate3._height;
crate9._y = crate3._y-crate4._height;
crate0._x = (crate0._width/2)+5;
crate1._x = (crate1._width/2)+12;
crate2._x = (crate2._width/2)+3;
crate3._x = (crate3._width/2)+15;
crate4._x = (crate4._width/2)+7;
crate5._x = crate0._x+crate0._width+5;
crate6._x = crate1._x+crate0._width+5;
crate7._x = crate2._x+crate0._width+5;
crate8._x = crate3._x+crate0._width+5;
crate9._x = crate4._x+crate0._width+5;
//give Rcrates Rlabels
this["crate"+m].content_txt.text = Rstack_array[m];
//ticks and crosses positioned on the crates
this["tick_mc"+m]._x = this["cross_mc"+m]._x=this["crate"+m]._x;
this["tick_mc"+m]._y = this["cross_mc"+m]._y=this["crate"+m]._y;
//make invisible
this["tick_mc"+m]._visible = false;
this["cross_mc"+m]._visible = false;
//make relevant one visible
if (Rtick_array[m] == true) {
this["tick_mc"+m]._visible = true;
} else if (Rtick_array[m] == false) {
this["cross_mc"+m]._visible = true;
}
}
Another bonus question then:
It doesn't seem to be possible to refer to each instance outside this for loop, in the same way (using 'this' etc). Is there another technique for this?
As an example I would like to clear the screen at a later stage of all the crates, ticks & crosses, without having to refer to each individual instance.
Many thanks!
if you want to clear them later just do something like:
for(var i=0;i<Rcounter;i++){
this["crate"+i].removeMovieClip();
//same for others also.
};
maximum rinse
10-18-2006, 03:02 PM
nice one, thanks tg!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.