-
hitTestObject from within an animated MovieClip help
Hi, I am having some trouble performing a hit test in a frog game I am making.
When the UP key on the keyboard is pressed an animation in my 'frog' MovieClip plays of a frog jumping, and at frame 22 and 23 (the peak of it's jump) a second nested MovieClip appears for those 2 frames with instance name 'mouth'.
I am trying to hit test mouth against 'fly' which is found on the main timeline.
Here is the code I have:
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, frogJump);function frogJump(event:KeyboardEvent)
{
if(event.keyCode==38)
{
frog.gotoAndPlay(2);
if ((Object(this).frog.mouth).hitTestObject(fly)) {trace("FLY HIT")}
}
}
I receive the following error when UP is pressed:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at FrogGame_Game_fla::MainTimeline/frogJump()
Thanks for the help.
-
Dignitary
"Object" is used to datatype a variable, simular to "Number" and "Event".
-
"similar", not "simular". Also, although casting this to Object is useless, it is not the problem here. The problem is that either there is no frog property (or it is null), or frog has no mouth property (or it is null) at the time that line is executed.
If you add either frog or mouth via addChild, that does not create a property to match. If you add them via timeline animation and the IDE, then you need to have their instancenames set, and "automatically declare instance variables" set.
But the problem is probably one of timing. After calling frog.gotoAndPlay(2), frog is not yet at frame 2. It just tells frog to go there. It still takes at least one frame for that change to take effect. And if the mouth doesn't get added until some time later, it will definitely not be there at that time.
Last edited by 5TonsOfFlax; 05-02-2012 at 10:16 AM.
-
Dignitary
Thanks 5TonsOfFlax for correcting my spelling. Keep up the good work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|