Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Possible to combine movieclip instances into a variable?

New Here ,
Feb 12, 2014 Feb 12, 2014

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!

TOPICS
ActionScript
310
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2014 Feb 12, 2014
LATEST

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;

}

}

.

.

.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines