Skip to main content
Participant
February 12, 2014
Question

Possible to combine movieclip instances into a variable?

  • February 12, 2014
  • 1 reply
  • 323 views

Hey! I'm making a game where I have 5 items (item1_mc, item2_mc, and so on...)

I had my game have the code repeating 5 times for each item which I feel was very inefficient.

if(cat_mc.hitTestObject(item1_mc)){

     score +=1;

          item1_mc.remove = true;

          item1_mc.x=-1000;

}

(Then the same thing for all five items)

And then:

if(score == 5){

     gotoAndStop(3);

}

Is there a way I can combine my instance names into one varible so I don't have to repeat the code multiple times? Thanks!

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
February 12, 2014

you could create a parent and add them to the parent:

var itemP:Sprite=new Sprite();

addChild(itemP);

for(var i:int=1;i<=5;i++){

itemP.addChild(this["item"+i+"_mc"]);

}

.

.

.

if(cat_mc.hitTestObject(itemP)){

//

but you're better off just using a for-loop in the test:

for(var i:int=1;i<=5;i++){

if(cat_mc.hitTestObject(this["item"+i+"_mc"])){

this["item"+i+"_mc"].remove=true;

this["item"+i+"_mc"].x=-1000;

}

}

.

.

.