The simple hitTest code, explained, and a lil’ bit improved.


Hi, I’ve seen many questions by many beginners asking about hitTest. Their questions are usualy about some simple hitTest codes, and I’m gonna explain for you how everything works in this tutorial.

WHAT IS HITTEST?

hitTest is a command used in actionscript to check if one object touches another. This is a very well used code, as it’s used in about every game. It can be used to create walls, powerups, traps or anything else that will do any command when the specified object touches it.

SO, WHAT’S THE CODE?

Um, no copy/paste, ok? This tutorial is meant for teaching you, if you copy/paste, you might get the code, but you will NOT understand it!

But the code is still here, and it’s explained too. It should be put on the enemy MC.

onClipEvent (enterFrame) { //When you enter the frame, code will run itself over and over again.
if (this.hitTest(_root.player)) { //Checks if it touches the player MC, in case it does, the following command will be executed...
_root.life -= 15;//You'll lose 15 health
}
}

Now, this isn’t the code I use normally, as it only checks the object rectangle, which means, if your MC is any other shape than a rectangle, the hitTest will be oversensitive. If you want to see the object rectangle, click on a MC with the selection tool. You will now understand, the player doesn’t have to touch the actual object, only that rectangle. This is why that code is a bad code…

THE CODE I DO RECOMMEND:

Well, let’s look at a better code. Put it on the player MC this time. And, no copy/paste!

onClipEvent (enterFrame) {//When you enter the frame, code is running itself over and over again
if (_root.enemy.hitTest(_x, _y, true)) {//The better hitTest, it do actually check if it touches the actual shape! enemy is the instance of the enemy MC
_root.life -= 15;//You'll lose 15 health
}
}

Well, this code does the same thing, but on a better way! It will check if it touches the actual shape, not the object rectangle. The first hitTest is really bad, and it’s not really easier either. Use this one, I would say it’s better!

Advertisements
Explore posts in the same categories: ActionScript 2.0, Basic

Tags: , , , ,

You can comment below, or link to this permanent URL from your own site.

3 Comments on “The simple hitTest code, explained, and a lil’ bit improved.”

  1. MIke Says:

    U explained what everyone does the same for 5 years in multiple tutorials. Try it little different. Explain how to achieve it for a car on the street (with angles).

  2. antoarts Says:

    MIke:

    Well, I know, but I explained this for beginners. Check out my other tutorials anyway!


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


%d bloggers like this: