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.
![]() Okay, now that you have your wall Movie clip, let's make it be a 10x600 rectangle like this:
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:
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: |
||




