Best viewed in Firefox

Awesty Productions

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!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

131 Comments »

Comment by Tom
2006-09-16 00:38:32

hey man, this tutorial was AWSOME :P sure ur drawings r a little crappy, but the lay out was sweet, thanks a bunch man.

 
Comment by awesty
2006-09-16 09:52:58

Glad you liked it, keep coming back and there will be heaps more soon ;)

 
Comment by neil
2006-09-17 04:14:15

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!

 
Comment by Andrew
2006-09-17 06:29:04

THANK YOU!!! I”VE BEEN LOOKIN FOR THIS 4 A WHILE!!!

 
Comment by DAVID
2006-09-18 12:24:36

HELP DOESNT WORK(MOVIE WILL NOT LOAD)SOME1 EMAIL ME DIRECTIONS PLEASE
gatemanornametag@hotmail.com

 
Comment by awesty
2006-09-18 12:27:02

send me the .fla and i will have a look at the problem ;) admin AT awestyproductions DOT com

 
Comment by DAVID
2006-09-18 12:29:05

no its that the movie on da…Um site dont load up T_T

 
Comment by awesty
2006-09-22 14:23:27

Have you got the latest flash player?

You can download it here:
Click here

 
Comment by fitskies
2006-09-23 12:43:54

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

 
Comment by awesty
2006-09-23 12:49:43

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;
}
}

 
Comment by teesha
2006-09-28 00:49:29

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

 
Comment by awesty
2006-09-28 10:34:41

Sorry but I don’t know what you mean by pencil. :?

 
Comment by troy
2006-10-03 23:35:23

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

 
Comment by haavard
2006-10-04 22:55:38

hi, thanks for a great tutorial the result was great! i fixed the actionscript there was a little mistake…

 
Comment by awesty
2006-10-06 17:12:31

@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. ;)

 
Comment by Jay
2006-10-16 00:25:58

Thanks!! It worked great!!!

 
Comment by DrP
2006-10-21 06:12:19

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!

 
Comment by awesty
2006-10-21 11:20:57

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;
}
}

 
Comment by Norm
2006-10-27 10:29:22

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! :)

 
Comment by awesty
2006-10-27 11:47:48

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;
}
}

 
Comment by norm
2006-10-28 00:51:05

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?

 
Comment by awesty
2006-10-28 10:22:20

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 ‘[]’ ;) )

 
Comment by Andrew
2006-10-29 02:44:46

cool tutorial but if you update it could you put a script for if you move over something it goes to the next frame

 
Comment by awesty
2006-10-29 10:06:21

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.

 
Comment by timmy
2006-11-03 15:20:57

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

 
Comment by awesty
2006-11-03 17:55:47

Make sure you have you have the right code on the right wall.

 
Comment by timmy
2006-11-04 14:00:38

actually i am confused
can you show me the
Top wall code
bottom wall code
left wall code
and right wall code
Thanks alot!

 
Comment by awesty
2006-11-04 14:31:57

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;
}
}

 
Comment by timmy
2006-11-04 23:07:20

Thanks man!

 
Comment by timmy
2006-11-04 23:12:27

it still doesn’t work… may i send it to you?

 
Comment by timmy
2006-11-04 23:16:25

err i got another question can flash make similes?

 
Comment by awesty
2006-11-05 10:08:34

Yea, send it to me if you want.

Do you mean smilies? Because I have no idea what similes are.

 
Comment by timmy
2006-11-06 14:33:25

I sended the file to you, anyways similes is things like when you chat on msn some animation that can show your feelings…

 
Comment by awesty
2006-11-06 15:35:44

I though they were smilies or emoticons. :?

 
Comment by timmy
2006-11-11 17:42:15

yea i think so
anyways my older verson cant read flash 7 what can i do?

 
Comment by awesty
2006-11-12 10:00:04

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.

 
Comment by timothy
2006-11-13 14:28:15

my flash verson is 5….

 
Comment by fjfjfj
2006-11-13 14:29:00

jjdd

 
Comment by awesty
2006-11-13 16:03:39

@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/

 
Comment by Flash Back Productions
2006-11-13 23:45:05

Thanks! so Much! now I can Make games

 
Comment by Webber
2006-11-15 11:52:36

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! :)

 
Comment by Webber
2006-11-15 13:12:06

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;
}
}

 
Comment by awesty
2006-11-15 15:22:33

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.

 
Comment by andy
2006-11-15 20:52:14

it dosen’t work. he goes right through the wall like water

 
Comment by John
2006-11-16 04:24:42

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.

 
Comment by MadFools: Bert
2006-11-16 08:13:12

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

 
Comment by Webber
2006-11-17 06:23:28

actually can u post that script here i wana no to! :p

 
Comment by awesty
2006-11-17 09:37:34

Just change gotoAndStop(2); to _root.gotoAndStop(2);.

 
Comment by Hats
2006-11-19 06:55:15

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

 
Comment by awesty
2006-11-19 09:20:10

I will do a tutorial today or tomorrow ;)

 
Comment by 0\/\/e|\|
2006-11-19 13:05:54

Is there a way to make an object or wall impassible on all sides?

 
Comment by Webber
2006-11-20 09:47:12

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)

 
Comment by awesty
2006-11-20 17:37:20

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.

 
Comment by webber
2006-11-21 01:26:16

sweet, why dont you just add it to your previous hit test tutorial?

 
Comment by webber
2006-11-21 01:28:04

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?

 
Comment by awesty
2006-11-21 15:14:23

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.

 
Comment by unknown
2006-11-26 00:38:37

nice one! i made a maze game with that one! thank you vm!

 
Comment by sam
2006-11-29 08:07:37

you typed “put this code in on top” twice :)

 
Comment by Teatles
2006-12-06 00:54:20

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?

 
Comment by teeno
2006-12-06 22:47:15

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

 
Comment by Tom
2006-12-07 02:56:02

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.

 
Comment by awesty
2006-12-07 15:19:33

@Sam: Yea… lol

@Teatles: You need to put stop(); on both frames.

@Tom: Please read the previous posts.

 
Comment by Tom
2006-12-09 02:37:27

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.

 
Comment by John
2006-12-09 04:03:31

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 !!

 
Comment by awesty
2006-12-09 09:44:17

@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;
}
}

 
Comment by John
2006-12-10 06:16:49

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 ?

 
Comment by Tom
2006-12-10 07:39:34

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

 
Comment by RDB2006
2006-12-10 10:43:51

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

 
Comment by awesty
2006-12-10 11:02:06

@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?

 
Comment by RDB2006
2006-12-10 21:40:07

Flash MX 2004

 
Comment by RDB2006
2006-12-10 21:41:28

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)

 
Comment by RDB2006
2006-12-10 21:44:42

i think :S

 
Comment by ya
2006-12-11 07:54:41

thanks! It was awesome!!!!

 
Comment by ya
2006-12-11 07:55:16

thanks! It is awesome!!!

 
Comment by awesty
2006-12-11 15:39:53

@RDB2006: Yes, you are correct.

 
Comment by sjfb.gf
2006-12-12 06:08:52

that was good but it din’t tell us how to do heath

 
Comment by bob
2006-12-12 10:35:38

i don’t see anything!

 
Comment by awesty
2006-12-12 15:51:54

@sjfb.gf: Thats a different tutorial:

http://www.awestyproductions.com/tutorials/

@Bob: Make sure you have the latest flash player.

 
Comment by Bryce K :P
2006-12-20 12:04:21

thanx awesty nice work couldn’t have done it better myself (even if i knew how :P lol)

 
Comment by Tai
2006-12-21 07:54:42

my man just goes passed the wall after its done sliding slightly to the left or right

 
Comment by awesty
2006-12-21 11:09:05

Make sure you have the right code on the right wall.

 
2006-12-21 20:03:06

[…] 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. […]

 
Comment by Spleen
2006-12-22 02:49:15

When i tried this, it had a gap between him and the wall, is there anyway to get rid of this gap?

 
Comment by Tai
2006-12-22 07:32:43

i did get the right code for the right walls and i aready took ur other tutorials

 
Comment by awesty
2006-12-22 11:01:22

@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.

 
Comment by RDB2006
2006-12-23 01:10:50

@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;
}
}

 
Comment by tai
2006-12-23 07:12:50

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

 
Comment by awesty
2006-12-23 12:28:48

Just email the .fla to here:

awestyproductions@gmail.com

 
Comment by kinto
2007-01-01 03:08:38

YOUR TUTORIAL IS AWESOME AND REALLY EASY TO FOLLOW!

 
Comment by Djo
2007-01-06 01:00:40

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

 
Comment by awesty
2007-01-06 11:58:47

I sent you an email.

 
Comment by kev
2007-01-11 01:53:50

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.

 
Comment by bananarchy
2007-02-02 22:03:32

thnx! from this basic tutorial i already have a gate that can open with a button O.o

 
Comment by Kurt
2007-02-07 11:43:50

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.

 
Comment by Phil
2007-02-17 01:50:21

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

 
Comment by Phil
2007-02-17 01:52:34

yur welcome Awesty & everyone else

 
Comment by Phil
2007-02-17 01:54:13

noone awnsers after me!!!

 
Comment by awesty
2007-02-17 12:49:23

@Kurt: http://www.wordpress.org

@Phil: Be patient :P

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.

 
Comment by Kaz
2007-02-21 03:42:33

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.

 
Comment by awesty
2007-02-21 15:31:19

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.