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 >:(
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.