Skip to main content
chienbleu
Known Participant
Feb 6, 2023 2:46 AM UTC
Question

TypeError: Error #2007: Parameter hitTestObject must be non-null.

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?

 

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

kglad
Community Expert
Community Expert
Feb 6, 2023 2:49 AM UTC

loop through all snake children and hittest each. ie, use numChildren and getChildAt in your for loop. 

Translate
chienbleu
chienbleuAuthor
Known Participant
Feb 6, 2023 1:31 PM UTC

like this?

 

for (var i:uint = 0; i < snake_ins.numChildren; i++)
if (snake_ins.getChildAt(i) is snake)
{
trace("asnltsatopgameal;ntaetnaen");
}

Translate
kglad
Community Expert
Community Expert
Feb 6, 2023 2:24 PM UTC

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 

Translate