HitTest check

I’m trying to create one of em shoot-em-up-games (like virtual cop)…
here’s what i got so far after a little while…


as u can see…the lil d00d is a little hard to miss…even though he’s a stick figure…(yea, just try shooting through his arms and missing)

y’s this so?

Flash will by default use the bounding box, not the content, to do the hit test, but you can add mc.hitTest(x, y, true); to test against the actual pixels of the clip.

You can try to cover it with buttons instead. That way, you can also have variable damage depending on where you hit the guy.

pom :asian:

Use for the actions for your man:

onClipEvent (mouseDown) {
if (this.hitTest(_root._xmouse, _root._ymouse, true){
// some code like _root.score++;

That’s what I use in my shoot the tweenie game - the link to my website is in my footer.

This method will check the actual pixels of the target mc.


mc.hitTest(x,y, true) will only check the x,y (ie. the registration point) of the target, not actual pixels of the clip.

For variable damage on the guy, use the same code as above, but break the guy down into, for example two movie clips, nested in one main one. One mc called head and the other called body. Increase the score by more if the head was hit.

P.S. I did submit a tutorial on how to create a first person shooter but nothing became of it…

ooo k…thx for the help…

anyway, what is the advantage of using hittest over the buttons method?

Well firstly, unless you’re using a custom cursor - you’ll get a rollover cursor - I use a sight and gun that follows the mouse - (you can download a fla on this from my site).

Also, making multiple enemies and making them mcs into buttons gets fiddly - you have more flexibility with actionscript for an mc, than with a button.

Anyway - it’s your game, but unless there’s a reason for it, don’t use the enemies as buttons because is much easier to do the hit animations as mcs - if you looked at the tweenies game you’ll notice that only if you hit one of them on the actual mc surface area, a hit will be registered, and the hit animation is played. This is much smoother and easier than sticking the mc in a button.

The code I posted can also be used to treat an mc like a button - an easy way to get rid of the roll over cursor.

But like I said before - it’s your game so use whatever you want.

Although Kirupa forum is an excellent resource for general flash tips (esp. for newbies), if you want advice on gaming only - check out the games forum at flashkit.

got that :slight_smile:


…You don’t even have to use the code that I posted. I’m obviously not up to date with the new event handlers for MX. You can use [COLOR=blue]on (press)[/COLOR], [COLOR=blue]on (RollOver)[/COLOR], etc. on mc’s just as you would for a button. So you don’t need to put the mc in a button, nor do you need to use my code for determining a hit test on mousedown, you can just use on press.

So instead of

[COLOR=blue]onClipEvent (mouseDown) {
if (this.hitTest(_root._xmouse, _root._ymouse, true) {
_root.score += 10;

You can just use

[COLOR=blue]on (press) {
_root.score += 10;

So you can use the normal button actions, and more, on a movie clip in MX.

oo k…didn’t know that…i just switched to flash mx from 5 :wink: