Hit Test

 
  This tutorial will show how to use the hitTest command to make walls that stop movement. This is done by making four if statements and four movie clips.
 
 

Create a Flash file of standard size and Actionscript 2

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:

Code:
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:
Code:
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:
Code:
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:

Code:
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:
Code:
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: