Copy link to clipboard
Copied
i'm making a game in AS3 in keyframe where there is a movieclip (killer) that has to collide with a child(trail) from the move clip(snake) . I like to call add "_mc" for movieclip and "_ins" for instance. Anyways this is my code
stage.addEventListener(Event.ENTER_FRAME, killsnake);
function killsnake(event:Event)
{
if (killer_ins.hitTestObject(snake_ins.children))
{
trace("asnltsatopgameal;ntaetnaen");
}
}
it keeps giving me the error. I'm not sure how to solve this without it saying "undefined property blablabla" or the error up top. What's a way I can do this?
Copy link to clipboard
Copied
loop through all snake children and hittest each. ie, use numChildren and getChildAt in your for loop.
Copy link to clipboard
Copied
like this?
for (var i:uint = 0; i < snake_ins.numChildren; i++)
if (snake_ins.getChildAt(i) is snake)
{
trace("asnltsatopgameal;ntaetnaen");
}
Copy link to clipboard
Copied
no:
for(var i:int=0;i<snake_ins.numChildren;i++){
if(killer_ins.hitTestObject(snake_ins.getChildAt(i)){
// do whatever other than remove children.
}
}
if you're going to be removing some of the children, loop backward:
for(var i:int=snake_ins.numChildren-1, i>=0;i--){
if(killer_ins.hitTestObject(snake_ins.getChildAt(i)){
// do whatever
}
}
p.s. your instance naming convention precludes more than one instance. it would be better to use a different convention. eg, killer_1, killer_2, etc
Copy link to clipboard
Copied
what naming convention should i use? I can't make the movieclip,instance and linkage all the same
Copy link to clipboard
Copied
i made a suggestion already for stage instances.
for linkage id's using a name+"_id" works.