Skip to main content
chienbleu
Known Participant
February 6, 2023
Question

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

  • February 6, 2023
  • 1 reply
  • 383 views

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 has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
February 6, 2023

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

chienbleu
chienbleuAuthor
Known Participant
February 6, 2023

like this?

 

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

kglad
Community Expert
Community Expert
February 6, 2023

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