Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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;
}
}
.
.
.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now