PDA

View Full Version : Issue with hitTestPoint and gotoAndPlay



Mickster
December 14th, 2009, 11:58 AM
Here's the situation: Your character - an ameoba-like thing (called "avatar" in the code) - has to avoid a crab-like thing (called "pincher" in the code). If they make contact, the pincher goes into an animation where his claws snap shut, and the avatar goes into a quick recoil animation. It looks like this:

if (avatar_mc.hitTestPoint(pincher_a.x, pincher_a.y, false)==true) {
this.pincher_a.nextFrame();
this.avatar_mc.gotoAndPlay("recoil");
}

else {
this.pincher_a.nextFrame();
this.pincher_a.gotoAndStop(1);
}

There's one problem: When the avatar makes contact with the pincher, the playback head seams to go to the correct place (the frame with the "recoil" label), but the avatar's animation stops. As long as he's in the collision zone with the pincher, he's frozen stiff (the pincher's animation works fine). Only after the avatar LEAVES the collision zone does his recoil animation play.

So here's what I'm guessing is happening: As long as the avatar is in the collision zone, his movie clip is getting repeated commands to go to the "recoil" label, preventing the playback head from moving past it. Does that sound right? What can be done?

creatify
December 14th, 2009, 01:12 PM
You're hit test function is running within an ENTER_FRAME event?

If that's the case, setting a flag is one way to avoid it from repetitively trying to play "recoil" - something like the following:



// outside of your enter frame function
var isColliding:Boolean = false;


if (avatar_mc.hitTestPoint(pincher_a.x, pincher_a.y, false)==true) {
if(!isColliding) {
isColliding = true;
this.pincher_a.nextFrame();
this.avatar_mc.gotoAndPlay("recoil");
}
} else {
this.pincher_a.nextFrame();
this.pincher_a.gotoAndStop(1);
isColliding = false;
}


Ultimately though, I'd actually remove the ENTER_FRAME when a collision occurs, then reactive that once the recoil animation finishes. Reason being, is it's a bit ugly to have isColliding = false; to continue to be set on the enter frame every frame.

Mickster
December 14th, 2009, 01:55 PM
That code works great, Creatify. Now, forgive the noob question, but how would I go about removing/reactivating the ENTER_FRAME? And why would it be ugly to have isColliding = false; to continue to be set - is this a performance thing? Because there are going to be a bunch of these pincher things onscreen at once and I'd hate to bog things down.

creatify
December 14th, 2009, 02:10 PM
That code works great, Creatify. Now, forgive the noob question, but how would I go about removing/reactivating the ENTER_FRAME? And why would it be ugly to have isColliding = false; to continue to be set - is this a performance thing? Because there are going to be a bunch of these pincher things onscreen at once and I'd hate to bog things down.

In your hitTest function, to remove the ENTER_FRAME (ie, stop it), you'd simply use:


if (avatar_mc.hitTestPoint(pincher_a.x, pincher_a.y, false)==true) {
removeEventListener(Event.ENTER_FRAME, yourFunctionName);
this.pincher_a.nextFrame();
this.avatar_mc.gotoAndPlay("recoil");
}


And, yes, that would be a performance thing. But, if you have one ENTER_FRAME event (as you should) that is checking a bunch of pincher, then removing it would stop all of them from checking for a collision. So, it really comes down to just planning how it all works. For example, I'd have a class for your pinchers, and then set a flag within the pincher that is colliding. Then, when the collision animation is finished, you'd unset that flag and that pincher would be available for another collision. Breaking things into classes like that will give you more control and end up allowing you to use less code at the end of the day.

Long story short though, since you're just getting into Flash, I don't know that I'd get caught up yet with complete optimization yet. The bigger fish to fry now is to just get your collisions working. Just know there are other ways to check collisions that will be optimum compared to repeating code that doesn't really need to be repeated every frame. Lets say you have 10-15 pinchers, then your code above probably won't bog, or bog a noticeable amount.

You might google or search the forums for collision detection, I'm sure there are some great examples out that that use hitTestPoint and will show some ways they're looping through multiple objects (pinchers) to test collisions.

Hope this helps.

Mickster
December 14th, 2009, 02:48 PM
"Long story short though, since you're just getting into Flash, I don't know that I'd get caught up yet with complete optimization yet"

Sounds like a good idea...the idea of creating external classes makes my head hurt at this point. And there will never be more than 8 pinchers onscreen at once, so I should be ok sticking with the code as is. Thanks a lot for the great help.