Skip to main content
Inspiring
March 26, 2013
Answered

Simple Hit test

  • March 26, 2013
  • 1 reply
  • 835 views

Hey I have a movie clip with a layout of

1. PEnemy

2. Enemy

3. enemy

then animations and on some frames a MC with an instance name of 'hit'

My movie clip animates and I want to detect hit Test with another child from another movie clip called 'you' with a child MC called 'target'

So bacicly I want

PEnemy.Enemy.enemy.hit to hitTest you.target

I dont understand hit tests that well in as3 This is what I have:

if (you.Target.hitTestObject(PEnemy.spawn.enemy.hit));

But it is not working

I also get this in output:

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

          at flash.display::DisplayObject/_hitTest()

          at flash.display::DisplayObject/hitTestObject()

          at eugenehoudin5AS3_fla::MainTimeline/EnterFrame()

This topic has been closed for replies.
Correct answer Ned Murphy

The code cannot target an object that doesn't exist, so you need to put in conditional logic to check if the object is null or not.  One thing you could also do to make sure the object always exists for the code is to have it present in all frames, but make it invisible in frames where you do not want it to be seen.  Then in your code you could test its visible property before trying to do the hit test - if it is invisible, skip the hit test.

1 reply

Ned Murphy
Legend
March 26, 2013

If hit only exists in certain frames, then chances are it is a missing object when that code tries to find it.  Put a trace in before that line so that you can see where in the chain the null comes to be.

Inspiring
March 26, 2013

Yea the hit can only exist in certain frames so how would I do that code wise

Ned Murphy
Ned MurphyCorrect answer
Legend
March 26, 2013

The code cannot target an object that doesn't exist, so you need to put in conditional logic to check if the object is null or not.  One thing you could also do to make sure the object always exists for the code is to have it present in all frames, but make it invisible in frames where you do not want it to be seen.  Then in your code you could test its visible property before trying to do the hit test - if it is invisible, skip the hit test.