[h1]
Hit Test[/h1]
Okay! This is the second tutorial! If you didn't take the first tutorial, I suggest you do now! Here's a link:
Basic MovementAnd if you
did take it, but
didn't save the .fla, you can download my source (my .fla) of it here:
*note* Please don't download this unless you've 1. Taken the tutorial, or 2. Understand the script I wrote.
Basic Movement Source FileWell! now that that's all cleared up, I'll give you a quick bio of this tutorial:
In my last tutorial, I explained the scripts used in basic movement, this was done by modifying the _x and _y position of a Movie clip (_x+=5; _x-=5; _y+=5 ;_y-=5). But I did this with some if statements. The strange thing is, I set 4 Boolean (true/false) variable, named _root.up, _root.down, _root.left, _root.right. I bet you're wondering
why I set those...well I'll tell you in this tutorial.
This tutorial will teach you how to use the hitTest command to make walls for the movement tutorial I made. This is done by making four if statement, and four movie clips.
hitTest Tutorial-Okay, here we go! Let's start by opening my BasicMovement.fla that you either downloaded (I recommend you do that, link's at the top of the page), or saved.
Okay, now that you have Flash open, and the BasicMovement.fla, you'll notice we have our 'hero'. If you care to test it (CTRL+ENTER), you'll notice that the arrow keys move him around, yay! But maybe we should give him some walls...because we don't want him going off the screen, and just stopping on the edge looks weird!
So, let's add a Movie clip.

Insert>New Symbol
It'll ask you to input the data, so make it look like this:

Okay, now that you have your wall Movie clip, let's make it be a 10x600 rectangle like this:

You can align it to the center like I did, but it's not important...
Okay, we have out Movie clip, so let's add it into our main frame with the hero! Go back to the main frame by clicking 'Scene 1':

Okay, now let's place our Movie clip onto the frame; let's make this one be out left wall, so it'll look like this:

If you notice I have a grid on, don't worry about it, I just like being neat ^_^, but if you must, CTRL+ALT+G, size 10x10.
Okay, we've got our left wall on, but oh no! There are no actions on it! *gasp*! Let's make it so when the hero hits our wall, it'll set the variable left to false, making this if statement on the hero:
if (Key.isDown(Key.LEFT)&&_root.left==true){
_x-=5
}
Not be true anymore, making the Movie clip unable to move left!
So, here's the action we need to put onto the Wall Movie clip that's aligned to the left:
onClipEvent (enterFrame) {
if (hitTest(_root.hero)) {
_root.left = false;
} else {
_root.left = true;
}
}
Now, let me explain this:
onClipEvent (enterFrame) { This is saying as long as this Movie clip is on the frame, the actions will run!
if (hitTest(_root.hero)) This is where hitTest comes in, basically, it's self spoken, if one Movie clip hits another, then it does the action!
_root.left = false This one is the action, if the previous statement is true (
if (hitTest(_root.hero))) then it sets the variable _root.left to false, making the hero Movie clip not move left!
But oh no! what if the Movie clip hero isn't hitting left, then what? It won't be able to move left anymore because it's still false!
So, we needed to add this stuff:
} else { Else basically takes the opposite of the if statement, example: if we said if (pie==true), then the else would mean if (pie == false).
_root.left = true; Now this script is nice and easy, it sets _root.left back to true!
Now, test the movie (CTRL + ENTER). Notice something wrong? It doesn't stop! =O Here's why:
The way hitTest works, is like this: if ([movieclip].hitTest([other movieclip)). So, I'm guessing you've noticed in the Properties tab:

Look to the left: you'll see something like this:

What the instance name is, it's like a reference. So go ahead and type the instance name 'hero', so then the hitTest will know what Movie clip we mean, when we say if (hitTest(_root.hero))! When you're done, it should look like this:

Good, now if you test it, it should work perfect...we just need to add some more walls! Here's what you do to make a top wall:
- CTRL + C or right click>copy on the Wall Movie clip
- CTRL + V or right click and paste on the main frame
- Now, go into Transform (CTRL+T) and rotate it 90°
- Next, position it to the top of the main frame, like this:

Now, we need to modify the actions, change the old action to this:
onClipEvent (enterFrame) {
if (hitTest(_root.hero)) {
_root.up = false;
} else {
_root.up = true;
}
}
Basically, all you did was copy a Movie clip, reposition it, and change the actions! Simple, eh? Okay, it's practically the same process to make a bottom and right walls, just copy, and past, the left wall, and align it to the right! Next, change the actions to this:
onClipEvent (enterFrame) {
if (hitTest(_root.hero)) {
_root.right = false;
} else {
_root.right = true;
}
}
Finally, we have our bottom wall. Copy and paste the top wall, and align it to the bottom, then, replace the actions to this:
onClipEvent (enterFrame) {
if (hitTest(_root.hero)) {
_root.down = false;
} else {
_root.down = true;
}
}
Now, let's test our walls, CTRL + ENTER and use the arrow keys!
Final product:
http://www.graphicaddicts.net/ssj/hitTest.swfIf you like this tutorial, please
Click Here to register.
Click the link below to download the Flash file for this tutorial.
hitTest Tutorial FLA