Making Walls with hitTests
September 3rd, 2006 by awesty
In this tutorial I will teach you how to make walls for you games using hitTests with actionscript.
What you need to know:
- Basic flash skills. Like using all the tools and everything.
- You need to have taken my ‘Moving MovieClips with the ArrowKeys tutorials. Click here veiw it now.
- It would help if you had knowledge of another programming language like PHP, JavaScript or C++.
I am using Flash 8 for this tutorial, but you can use Flash MX 2004 if you want.
Lets begin!
Thanks for taking the time to veiw this tutorial!
Filed under Flash Tutorials















hey man, this tutorial was AWSOME
sure ur drawings r a little crappy, but the lay out was sweet, thanks a bunch man.
Glad you liked it, keep coming back and there will be heaps more soon
Thanks for the tutorial, its getting me finaly UNDERSTANDING actionscript rather than just copying and pasting it, so that i can start to write my own. Thanks!
THANK YOU!!! I”VE BEEN LOOKIN FOR THIS 4 A WHILE!!!
HELP DOESNT WORK(MOVIE WILL NOT LOAD)SOME1 EMAIL ME DIRECTIONS PLEASE
gatemanornametag@hotmail.com
send me the .fla and i will have a look at the problem
admin AT awestyproductions DOT com
no its that the movie on da…Um site dont load up T_T
Have you got the latest flash player?
You can download it here:
Click here
(My name is not fitskies in real life) I spent 2 hours carefuly puting in the action scripting and when i tried it the character just walks trough the wall apart from the one on the left the character just slides out of the box >:(
you need to get the minus number to match the objects speed. if the code says ._x -=5 and your character’s speed is set at 10, he slides through at a speed of 5.
whoops, i just realised i mucked up a bit in the tutorial.
on the right wall put this code:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 5;
}
}
and on the left one put this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x = 5;
}
}
Hi there , this is regarding the tutorial pencil . i tried it the drawing actually moved but the thing is i couldnt get the pencil to draw i draw the pencil is still” PLEASE HELP
Sorry but I don’t know what you mean by pencil.
i did the tutorial, but my guy does not stop at the walls and just goes right through, im not sure what i did wrong though
hi, thanks for a great tutorial the result was great! i fixed the actionscript there was a little mistake…
@troy: Sorry but I have made a mistake in the tutorial. I am currently away and do not have flash on the computer I am on. Please read the comments above because it says how to fix it.
When I get back I will fix the tutorial.
Thanks.
Thanks!! It worked great!!!
Nicely explained tutorial but I’m having the same problem.
My square goes through the wall if you keep the arrow pressed. If you release the arrow while the square is behind the wall, though, it works fine!
Try doing this:
on the right wall put this code:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 5;
}
}
and on the left one put this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x = 5;
}
}
Hey.. was working fine earlier but now as soon as I put the code for the right wall in, when I load the application the wall starts sliding to the left! Help!
Try doing this:
on the right wall put this code:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 5;
}
}
and on the left one put this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x += 5;
}
}
Still no
That’s what I already had. It seems that if I type any errors in the code, the wall stays still but nothing happens when the “man” collides with it. However, when I do everything correctly, the right wall starts moving to the left. Interestingly, if I put:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 25;
}
}
as the code, the right wall moves faster to the left, as if the _root.man. is referring to the wall itself?
Make sure you have the instance name man on the right MovieClip.
If you want you can send the .fla to admin[@]awestyproductions[.]com. (Without the ‘[]’
)
cool tutorial but if you update it could you put a script for if you move over something it goes to the next frame
Just use this code:
onClipEvent(enterFrame){
if(this.hitTest(_root.NAME)){
_root.gotoAndPlay(FRAME);
}
}
Just replace NAME with the instance name of the movieclip and FRAME with the frame you want it to go to.
when my man wnet near to the left wall it just got pushed out the walls i don’t know what i did wrong. Thanks
Make sure you have you have the right code on the right wall.
actually i am confused
can you show me the
Top wall code
bottom wall code
left wall code
and right wall code
Thanks alot!
Top wall:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._y = 5;
}
}
Left:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x = 5;
}
}
Right:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 5;
}
}
Down:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._y -= 5;
}
}
Thanks man!
it still doesn’t work… may i send it to you?
err i got another question can flash make similes?
Yea, send it to me if you want.
Do you mean smilies? Because I have no idea what similes are.
I sended the file to you, anyways similes is things like when you chat on msn some animation that can show your feelings…
I though they were smilies or emoticons.
yea i think so
anyways my older verson cant read flash 7 what can i do?
Umm, what version of flash were you using when you sent it to me?
I only have Flash 8 so I can only save it as a Flash 8 or Flash 7 (MX 2004) file.
It wouldn’t work if you are using any previous version any way because it is a different version of actionscript.
my flash verson is 5….
jjdd
@timmy: Then this won’t work. Flash 5 uses ActionScript 1.0, Flash MX 2004 and above use 2.0 and the new Flash 9 is going to have ActionScript 3.0
http://www.awestyproductions.c.....cript-30s/
Thanks! so Much! now I can Make games
hi first i wana say thanks most work fine for me (im a little slow
) anyways, if i wanted to make a maze would i have to make center test hit or would it just be bottom etc from were its closest to?
ps: my business class is doing a project with flash so this is suuuuuper inportant it wouldnt be cheating just lots of help!
PS: Westy you havent been noticing but uve been forgetting in the tutorials (or here in comments) the and - parts before the = 5;
so heres all the correct ones
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._y = 5;
}
}
Left:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x = 5;
}
}
Right:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 5;
}
}
Down:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._y -= 5;
}
}
I haven’t been forgetting them, for some reason they get taken out like yours. :\
I will try to do an advanced hitTest tutorial that will explain how to make a maze type thing.
it dosen’t work. he goes right through the wall like water
Its awesome! I’ve been looking for this for awhile for my Rat ‘N’ Cheese Game (COPY RIGHT O_O). And for those of you who have errors, be sure to follow every step and dont delete any of the code. Make sure you have the -’s and ’s along with y’s and x’s. Also, I have a couple questions that I need to ask, could you E-mail me at kbb101@mail.com ? I may not see your reply here since I don’t come here often, and I’m mostly off the internet.
Dude, how would you use HitTest to make it so that when one thing collides with another, the WHOLE movie goes to another frame. I tried to do it with the following actionscript
onClipEvent (enterFrame) {
if (this.hitTest(_root.man)) {
gotoAndStop(2);
}
}
but it doesn’t work. e-mail me at MadFools.Bert@gmail.com
actually can u post that script here i wana no to! :p
Just change gotoAndStop(2); to _root.gotoAndStop(2);.
Hey, this has been a great help. I know its a bit off the plot but could you tell me how to make an MC that slows down or speeds up the little guy?
Cheers
I will do a tutorial today or tomorrow
Is there a way to make an object or wall impassible on all sides?
yeah just make the layer make it mc then use the black cursor to select each side, put the action script of the apropiate side and it should work (i havent tested)
That would work, but there is a easier way. I will write a tutorial for the other way but I have heaps of tutorials I want to write that I don’t have time for.
sweet, why dont you just add it to your previous hit test tutorial?
also, i noticed you can go thru the wall if you have enough speed, im making a gravity type game, so how do i make it so its just simply “impossible” for the ball to go thru it at ANY speed?
Because if people have already done this tutorial they aren’t likely to check back, but if they see there is a new one they might.
With the walls, your walls need to be thick. If you wall is only 2 pixels thick and your MC is traveling at 10 pixels, it is going to go straight past it without even touching it. So try to make your walls thicker than the speed the MC is traveling.
nice one! i made a maze game with that one! thank you vm!
you typed “put this code in on top” twice
i put the code in for making the movie go to a different frame when two objects touch, but the movie just plays and repeats from the start, and flashes back and forth, so i cant move my character, because its just repeating these two frames instead of staying on one until the objects touch. how can i get that to happen?
hey thnx a bunch agen i looked at ur movement tutorial and they are so easy to follow i have now made a maze game thnx it was hard work and tokk a long time im loking now to add little monster trap things which take down my health . thnx
Hey, I’m having trouble with the hit test, I want to make it so my character that i move can’t go through a wall from either side, i tried to put in the coding for the X and -X, but it seems to slide from the right and work from left, please help xD.
@Sam: Yea… lol
@Teatles: You need to put stop(); on both frames.
@Tom: Please read the previous posts.
Hey, i tried to make a door where if it roots with my man it goes to the next frame, i used this coding;
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.gotoAndStop(2);
}
}
But it just goes and plays at Scene 2, when it’s supposed to play Scene 4 frame 2.
and i’ve already tried .gotoAndStop(”Scene 4″, 2)
But it’s not working, any help would be great thanks.
Hey good tuto, but i have a problem.
Whent the movie is tested, the man just walks through the left and top wall and gets stuck inside it. This is annoying me alot. Please help !!
@Tom: Try changing _root.gotoAndStop(”Scene 4″, 2); to gotoAndStop(”Scene 4″, 2);.
@John: Change the top wall to this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._y = 5;
}
}
And the left to this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x = 5;
}
}
Ermmm ive tried the new code and ti still does not work it still is just walking through. I think it might be something to do with the fact that I have Flash MX 2002. Is there anything I can do ?
John i’m having the same same problem and i have Flash MX, But i’m sure there’s a way of doing it :S
im havin the same problem with the left wall, my char keeps going through it and gets stuck. My top, right, and bottom wall are fine tho. Any help? ty
@John: If you have any version of flash previous to Flash MX 2004 and any version more recent then Flash 8 (which you shouldn’t since there isn’t) then it won’t work.
@Tom: Again, you need Flash MX 2004 or Flash 8, they have a different version of ActionScript.
@RDB2006: What version of flash are you using?
Flash MX 2004
w8 a min.. i think its cuz u have the wrong code for the left wall (You have the right walls code for the right wall, bottom for bottom, top for top, and top for left. it should be the left code for the left wall)
i think :S
thanks! It was awesome!!!!
thanks! It is awesome!!!
@RDB2006: Yes, you are correct.
that was good but it din’t tell us how to do heath
i don’t see anything!
@sjfb.gf: Thats a different tutorial:
http://www.awestyproductions.com/tutorials/
@Bob: Make sure you have the latest flash player.
thanx awesty nice work couldn’t have done it better myself (even if i knew how
lol)
my man just goes passed the wall after its done sliding slightly to the left or right
Make sure you have the right code on the right wall.
[…] This tutorial will be a whole lot easier if you have already taken my Moving MovieClips with the arrow keys, Making walls with hitTests and Making a health system tutorials since alot of the script used in those will be used in this tutorial so I won’t go into as much detail with them as I might with the rest of the script. […]
When i tried this, it had a gap between him and the wall, is there anyway to get rid of this gap?
i did get the right code for the right walls and i aready took ur other tutorials
@Spleen: Yes. Try my other tutorial on hitTests. It is much better.
http://www.awestyproductions.c.....ts-part-2/
@Tai: Make sure the character isn\’t moving faster than the walls are repelling him.
@Tai if your wall is on the right hand side try putting this code on it:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x -= 5;
}
}
if its on the left hand side try putting this on it:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x = 5;
}
}
my man still goes through the wall this is going to be the last thing i need to do for my first stage of my fighting game
Just email the .fla to here:
awestyproductions@gmail.com
YOUR TUTORIAL IS AWESOME AND REALLY EASY TO FOLLOW!
Yo awesty..
nice tut.. im not a programmer at all
im more of a designer animator
i was wonderin of u could help me out with
some AS 2.0 scripts for my first game..
http://www.djo-e.com/TEST/game.html
click to view a preview
if your intrested
please mail to infodjo-e.com
I sent you an email.
nice tutorial man but u should make better drawings but still the tutorial is sick and it finally worked not like the health system 1.
thnx! from this basic tutorial i already have a gate that can open with a button O.o
Sorry that this is really off topic but i want people to comment the games i create for my website, How did you get that blog or what html did you use i hope you can answer.
Kurt.
Kurt how much izzit for a website?
also Awesty 2 things how is yur name pronounced &
I wanna make the best game ever. if i go 2 times
a day how long wood it take?
a month or 2?
every one else
the = 5
should be
plus= 5
plus is plus symbol
yur welcome Awesty & everyone else
noone awnsers after me!!!
@Kurt: http://www.wordpress.org
@Phil: Be patient
My name is pronounced:
A - westy.
The A is separate. Btw awesty isnt my real name, it is kind of a combination of my first and last name.
And it depends on how big the game you are making is.
I was just wondering, my wall makes the object that i’m moving jump off it, it doesn’t hit it and stop there, also if I have speed up the object that I’m moving it goes through the wall.
Your walls need to be thicker than the speed you are moving. If you are moving at 10 pixels a frame, and the wall is only 2 pixels the MC will go straight through the wall.
Ok thanks =D
How do I ensure that it is exactly 25 pixels, is there a box I can fill in or something?
@Anon: There is a box in the properties panel.
hey…cool tute…im making a car racing game and i have two cars…(p1 and p2) i was wondering how to make it so the hit test on the walls can be for both cars…for example this would be for 1 car…
onClipEvent (enterFrame) {
if (this.hitTest(_root.car1)) {
_root.car1._x -= 5;
}
}
so how do i make it for car1 and car2?
i also need some help with codes for the finish line…so it can detect who wins and only shows the winner screen after say 3 laps…hope u can help
sam.
@sam: You could just have two separate if statements for each car. So like if(this.hitTest(_root.car1)){ and if(this.hitTest(_root.car2)){.
For the finish line part, you could do something like:
if(this.hitTest(_root.car1)){
_root.gotoAndStop(WinFrame1);
}else if(this.hitTest(_root.car2)){
_root.gotoAndStop(WinFrame2);
}
But obviously you have to change WinFrame1 and WinFrame2 to actual frames.
it is not working. all that happens is it slows down a little than it goes through. what should i do.
Make sure the speed it is being repelled by the wall is equal to or less than the speed the MC is moving.
Great Tut! As said above, now no more copy/paste, jsut understand, UNDERstand, UNDERSTAND!
set(Awesty = Amazing)
Awesome tutorial.
The best part is all the spelling and grammar mistakes.
You is be good of Flash, is like to maek rectanlges be good.
it doesn’t work, it just pushes my man.
nvm i got it to work
hello i put the code in correctly but it doesnt work. what happens is — if the man doest go the out if the stage fully when it hits the walls it comes back in automaticly but if it does go out of the stage completly it doesnt come back.
PLEASE HELLLLP ME
Didn’t read all the comments but for those of you having problems with the hero sliding through the wall… I gave it a thought and at the code:
and on the left one put this:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.man._x += 5;
Hey Awesty (if that’s really your name…)
I’ve made walls for my game, but I need the char to not to enter from any side.
- I’m guessing that what I have to do is subtract the char’s x/y coordinates by the wall’s and if it’s pos/neg, a boolean will tell the script to ‘move’ the char in the appropriate direction. Is this right? How do I actually do this?
- Another problem is that using the basic ‘one-sided’ walls you explained in this tut, it is successful in stopping my char from walking into the wall, but not the stage(which follows the character around) I know the code for the stage to move but not how to get the wall to stop it.
Here’s what’s on the wall:
onClipEvent(enterFrame){
if(this.hitTest(_root.man1)){
_root.man1._y -= 4;
_root.Stage._y = 4;
}
}
This doesn’t work to stop the ‘camera’.
If you don’t get what I’m saying, just ask for me to send an fla.
Thanks for the help.
whoops, there’s a mistake…
_root.Stage._y -= 4;
should be:
_root.Stage._y = 4;
(The code still doesn’t work correctly ofcourse)
‘+’ doesn’t work? :/
I understand exactly how to do this. But what if I wanted, well I do, to do this with a cursor. I tried using the same AS but it wont work. What can I use?
I don’t get what you mean.
You want the cursor to collide with the walls?
No, I have a character which moves using the arrow keys, I just don’t know a simple way to stop the char walking through the walls from all sides.
Sorry, but you have to use actionscript.
There is a program which allows you to make games without any code. It’s called GameMaker. If you google the name its website should come up.
hahaha
no no I understand Actionscript. If you look at my rpevious comments I have already posted code up there… I’m just asking for the code to stop the char if he hits any side of an object (he cant enter the object from any angle), ex:
char moves diagonally and hits the wall, he can never overlap with the object. Do you know how to code this?
btw, that guy up there who talks about stopping his cursor is NOT me, I’m Boston only
sorry for the confusion, but I’d also like to know what code you’d need to do what he’s asking for too…
Oh, okay.
I have written a tutorial for that. You can find it here:
http://www.awestyproductions.c.....ts-part-2/
fast reply! thanks
Ooooh. Duh. Why didn’t I think of that? I thought it would be more complicated. Is there a way you can have the wall as a movie clip, place it in the middle, and have it block you on all sides?
THE BEST TOTURIAL EVER!
jugar poker en linea…
No es importante dados poquer juegos poker shareware…
i was doing another one of your tutorials and it put a fake gravity on my guy and the bottom barrier dosn’t keep him in the frame. how do i fix it?
:/
Hi, would you know how to get a ball to bounce off a character that is moving around the screen? I have it bouncing off the walls, but cant get it to bounce off my character.
Thanks
wow this is what i was looking for but if you go on the right he goes through the wall
thanks everyone for making adjustments
HI THERE
im having a problem
im trying to make a maze game but the man
keeps going through the other
sides
and i can’t stop the man from going through each sides of the wall.
can you help me?
-julian
This helped me a lot, but when i comes near a wall cant get away from it
WHAT DO I DO FOR BUTTON TO GO TO NEXT KEY FRAME AND HOW NAME!
error:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Syntax error.
onHitGoto”2″
Total ActionScript Errors: 1 Reported Errors: 1
help!
try this:
on(release){
gotoAndStop(2);
}
hope this helps
thanks, this has realy helped me. A realy usefull thing, I;d never sigured out how to do this withouth help
happy to help
Hey that code doesn’t work for me… I still walk through it like it isn’t there. What do you think could have gone wrong? Anyone, PLEASE HELP!!!!
Oh, yeah… I converted it into a symbol and applied actionscript to it but it still doesn’t work.
hi i have put the code in the wall and it works fine. by changing the value you can make your character iether stop, slow or bounce off. but what if ound is you can not make a 2nd wall that stops it from moving left. for example only one wall can stop each direction up, down, right, left. if i make a second wall to stop it moving lelft it will go strait through that one. anyone no how to fix this
Hi,
I just downloaded flash pro 8 and is quite good but the actionscript box always says that i cant write in it even when somethings an symbol! Its really annoying i wonder if anyone can help?
Hey thanks, this made my creation a breeze!
cheers, man.
for so damn long iv’e been looking for a tutorial that does this and i cant believe no one else has actually created a tutorial for this. i search for this but all i find is a collision detection just saying “colision” and no wall code.(no simple one anyway). thanks dude u rock!