SyntaxScrewer's Blog

February 26, 2010

Collision Detection Tip – Using nested mc’s

Filed under: Flash,GameDev — geekymaira @ 10:55 am

I was trying to get a bullet to collide with a bug(enemy) on the stage. The bullet_mc had a small animation which shape tweened the bullet into a splash.
I wanted this animation to play when the bullet hit the enemy and appear on top of the bug, to depict that the bug had been squatted by the bullet.
So I detected the collision using hitTestObject , stopped decrementing the Y for the bullet (To stop it from
moving upwards) , and called the bullet_mc.play() function on an active collision to play the splash animation.

But, I ran into some problems. Since, the hitTestObject works with bounding boxes, my bullet was colliding with the enemy a little too soon.This was because a collision took
place as soon as the top of the bullet bounding box hit the bottom of the enemy box (Yeah, my enemy was on top, the bullet came from below). Thus, when the bullet_mc.play()
was called, the splash appeared out of place (where the mc had stopped) rather than on top of the bug (where I wanted it to 🙂 ).

The solution was pretty simple and an obvious one, but I still had to find it on IRC :P. I made a tiny 1 px by 1px mc inside the bullet_mc. Then instead of detecting a collision between bullet_mc and the enemy_mc, I
detected a collision between this tiny MC and the enemy_mc. With a little tweaking in the position of the tiny_mc inside bullet_mc, I was able to play my animation on top of the bug
like I wanted :).

You can take a look at the problem I faced here : What was wrong ?
You can take a look at the solution here : How was it fixed ?

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: