Using hitTests Part 2
In this tutorial, you will learn how to use a different kind of hitTest to the one I used in the other tutorial. This one can save alot of time if you want to make something like a maze.
I am using Flash 8, but Flash MX 2004 will work fine. Any version previous to that won’t.
Here is an example of what we will be making: Link
First of all, open a new flash document. You need to draw a simple maze type thing, for your character to try and get through. Here is what mine looks like:
Now make it a Movie Clip and give it an instance name of ‘maze’.
Making it a MovieClip:
Giving it an Instance Name:
Make a character that will be moving through the maze. Make it a movieclip and give it an instance name of ‘man’. (See above for examples.)
Put this code on it:
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
this._y -= 3;
}else if(Key.isDown(Key.DOWN)){
this._y += 3;
}
if(Key.isDown(Key.LEFT)){
this._x -= 3;
}else if(Key.isDown(Key.RIGHT)){
this._x += 3;
}
if(_root.maze.hitTest(_x+(_width/2),_y,true)){
this._x -= 3;
}
if(_root.maze.hitTest(_x-(_width/2),_y,true)){
this._x += 3;
}
if(_root.maze.hitTest(_x,_y+(_height/2),true)){
this._y -= 3;
}
if(_root.maze.hitTest(_x,_y-(_height/2),true)){
this._y += 3;
}
}
That looks longer than it actually is. It is all just repeated though, with a few numbers changed around.
The first part you should already know, but if not, you can find an explanation on it in this tutorial. LINK.
The part I will be explaining in this tutorial is this part:
if(_root.maze.hitTest(_x+(_width/2),_y,true)){
this._x -= 3;
}
That is just a if statement. What it does it is checks if _root.maze (the MC with an instance name ‘maze’) is hitting this movieclip. This hitTest is more precise than the other one though. There are 3 parameters. target.hitTest(_x,_y,shapeFlag). Target, is obviously the movieclip the hitTest is for, which in this case is ‘maze’. The first parameter is the _x coordinate of the other MC. In this case, it is ‘man’s’ _x coordinate, + half its width. Since the registration point is in the center of the MC, that is where the _x coordinate is. But if you add half its width/2, it is going to be on the right side of the MC. So then the hitTest only counts of the ‘maze’ hits the right side of ‘man’.
That was hard to explain, so sorry if you didn’t catch all of it. It’s the same for the rest, but with some numbers changed around so it is a different side.
That is all for this tutorial. You can download the .fla here: LINK















this helpped me sooooooooooooo much thx.
Good to hear
cool tut, just one question how do you make a win feature?
yep a gd tutorial, but just one problem. Most of it works, and in different areas the man goes into it a little bit, and at a certian point it wont go through. It might be because my edges are a bit thick or too smooth? any help?
i just tried it agen with straight lines, but it goes through more of them, right out into the other side :S
@Oakhart:
You could have a movieclip at the end and put this code on it.
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.gotoAndStop(2);
}
}
And on the second frame have a win screen.
@RDB2006: Make sure the lines aren’t to thin and the ‘man’ MC’s registration point is in the center.
First off i wanted to say that you truely are gifted and have helped me very much in this and the other tutorials of yours i have read.
I was wondering though if you could explain how to make a controlable symbol(using up,down,left,right) where each time you change directions your symbol is animated, and he is in just a standing position when a key isn’t being pushed.
what i did to that in my games is i made q movie clip inside it of what the arms do and the ationscript is stop(); FOR EVERY frame of the arms moving. now make 4 framews in the character’s movieclip each with him and the arms facing a different direction. make the arms movie clip instance name “arms”.
Last of all, in the Key.(whatever)part after the brackets do this.gotoAndStop(the frame you want it to go to inside the frame); and this.arms.play();
sorry if it doesn’t make sense but don’t thank me if you do understand.
follow this! http://www.bounceboy100.deviantart.com/
look ’round there and you might see!
i second that, this is the best tut site iv ever ben to, unsted of just copy pasting info you teach this info. and answer our quest, weather theve ben asked 1 or 5 times. and your are nice and smart- AWSOME SITE!
@Bulletluigi: You could try these two tutorials:
http://www.awestyproductions.com/tutorials/flash-tutorials/making-an-rpg-type-game/
http://www.awestyproductions.c.....ight-game/
@Oakhart: Thanks
Wow you really helped me alot on this one Awesty. I’ve made two levels so far, both with patrolling gaurds, that if you touch you go to an interactive maze game over screen. I can’t wait to finish it and publish it to my dA.
I also can’t wait to see what else you’ve got up your sleeves!
Ty for your tutorials awesty, ive read them all and theyve helped me a lot progress in flash. Ive made a cave raiding game with lava, traps, walls, treasure etc. So ty nd i’ll publish it soon on my site when ive finished
Well.. how do you make 1 frame be animated?
I understand that i need to do the gotoAndStop(FRAME), but i dont know how i can have an animation of walking in 1 frame. To make something animated i thought you had to have multiple frames..?
Please help me if you understand what i have asked now.
I have a question i sent to your mail, but since is not working…
can i do this:
_root.XX_mc.gotoAndStop (var)
i think it’s pretty clear, but what i want to do is to, at some point send a movie clip to the frame equal to a variable, i also tried:
_root.XX_mc._currentframe = var
but none of them worked, i’d apreciate any help, thanks in advance
**Error** Scene=Scene 1, layer=button, frame=1:Line 1: Statement must appear within on handler
if(lvl.Boolean = true){
Total ActionScript Errors: 1 Reported Errors: 1
What is wrong with this? lvl is a variable
shouldn’t it be == instead of = so that it checks if it is equal instead of making it equal? i know nothin about booleans but i wanted to point that out!
@Bob: I don’t know about you, but I have arms up my sleeves.

@RDB2006: Sounds cool. Can’t wait to see it
@BulletLuigi: You need to make a MC (Movie Clip). Hit Ctrl F8 to make a blank one. To edit it, open the library panel (Windows>Library I think, if not try Ctrl L) and double click it.
Now you should see a blank timeline. Make you animation there, and then go back to scene 1 (above the timeline). Now, drag your MC onto the frame. Now when you play the movie, on that frame, the MC will play, even if the movie is not. It is kind of like a movie inside a movie.
With the error, is that inbetween onClipEvent(enterFrame){}?. If not it needs to be. Also you don’t need the .Boolean on the end of that variable. You can just use lvl.w
@Blizzard: Here is a copy of the email I tried to reply back to you.
Instead of doing _root.bzzd._currentframe = percentage, you could do
something like _root.bzzd.gotoAndStop(percentage). But here is a good
tutorial for preloaders, and it is easier than making 100 framed MC’s. It
only has 3 frames I think.
http://www.graphicaddicts.net/.....425.0.html
Hope that helps,
Awesty.
Wow.. thanks a lot man you truely are the best.. lol but its like 3 am here.. what country do you live in? (if you don’t want to say just ignore this post)
onClipEvent (enterFrame) {
facing = this._xscale;
if (Key.isDown(Key.RIGHT)) {
_x = 5;
gotoAndStop(2);
facing = 100;
}
if (Key.isDown(Key.LEFT)) {
this._x -= 5;
gotoAndStop(2);
facing = -100;
} else if (!Key.isDown(Key.LEFT) &&
!Key.isDown(Key.RIGHT)) {
gotoAndStop(1);
}
}
I was really proud of myself for thinking of this script on my own but then.. I tested it and when I press left the Hero’s _xscale doesn’t make him reverse direction to face left.. What did I do wrong?
Well.. i got it to work right, but i still dont know why the script that i posted didn’t work.. all i did to fix it was change where i put facing = (percentage) into this._xscale = (percentage) , but i thought it would be the same thing because it was a variable that meant this._xscale. Please help me understand this.
Awesty,
i know that doing 100 framed mc’s may not the best way, but i have an animated logo, and the thing i want to do is to animate it as the movie is loaded, if the percentage loaded is 15%, i want the clip to go to frame 15, that way, the the clip would be advancing as the movie is loaded, i’ll try the code u gave me, i had think about it but i wasn’t sure if you could put a variable there…
Thanks
2 more things:
-I’ve managed to do what i wanted =D, shall i send u the .fla file?
-No signs of your mail, maybe i screwed up, i don’t know if the “.mx” matters at yahoo, i just keep forgeting to write it… =/
thanks a lot =)
@Bulletluigi: It is 10:15am here now, I am not sure what time it was when I posted that though.
With the variable thing… Try not to use variables for functions or properties, just use there real names
.
@Blizzard: Do it which ever you think will work best for you, I just like the other way better. Many people will like your way better.
Are you using Flash MX?
Btw my mail isn’t working at the moment, but it should be fixed soon.
Yea, I just like to trial and error and learn new ways of doing different tasks.
Oh yea I meant to ask this before, because its too late now, but did you enter a game into the Armor games contest? If not why don’t you? You would have a good chance to place.. and though it isn’t much to offer. If you decide to do a contest in the future i would be more than happy to help you out(even though the main reason that I want to do something like this with you is so that I can learn from watching you script and if at all possible I could help you). I can do animation alright I guess if you needed that. Well just send me an e-mail explaining your answer to this message. Bulletluigi@hotmail.com
No, I didn’t enter into it.
I started a game. Set out the map basically and every thing. Just needed to code it, then put in the good graphics. But I started to late, on the 10th of December about so I didn’t have the time or motivation.
Yea I understand man, but they do these contests often I have been told, so if you ever need help with graphics or animations then i’ll be more than happy to help. I’ll start working on a few animations and characters and such to help you decide if you’d want my help or not.
(I don’t know where to post this because it is off subject for like everything, but .. )
For Christmas I might be getting one of those drawing pads that you hook into your computer. I am also getting a Wii!! WOOT! Twilight Princess FTW!
yey!, i know how to make a maze game!
Great tute
THNX!
hey
how do are I make an enemy, who follow the man in the maze game?
OH yea awesty if were anywhere close to done with that game i’m sure everyone would want you to finish it. =^.^=
oh yeah, never thought of it, how can i make an enemy in this game… or should i just look at ur A.I. tute?
@Bulletluigi: You mean a tablet? Btw I really want a wii and TP, it looks so good.
Newgrounds usually have a contest on at least every 2 months, or more often.
Armor game don’t have them as often, but if you can make a good game they will sponsor it and you get paid good $$.
And no, I am nowhere near finished.
@KIM & awestyfan:
http://www.awestyproductions.com/tutorials/flash-tutorials/basic-artificial-intelligence/
http://www.awestyproductions.c.....ce-part-2/
i need help (i need an ans ASAP(i dont know why))
how can i make my man mc unable to go to the finish line without a key?
sorry about that, but i figured it out
where can i put my game for every one to play?
Thanks dude! this is wat i look for get start my game design..thanks again..:)
@KIM: Try
http://www.newgrounds.com - Your game might get blammed if its not good enough.
http://www.deviantart.com - All art/animations/games get accepted.
tnx!!!
this is also good for ground and walls like in mario thanks
nice this helped me to understand something new
is there a way to make it go to a different frame when it hits the walls
if(this.hitTest(_root.wall)){
_root.gotoAndStop(FRAME);
}
i need help creating a footbal game.
i cant do the goals
if(this._y > 385){
_root.lose();
}
if(this._y
The code gets cut of. Just replace the < with LESSTHAN. Since only I can use <
hiya awesty I am VERY VERY new to flash and i am trying to make my own rpg game, and ive read all of your tutorials but i dont understand how to make an animation of him walking, jumping or kicking or hiting or anything like that. could you please tell me how to do it please cause I just can’t do it. But ive seen how smart you are so I thought you would be able to help me.
Hope you help
Thanks
Calum
oh and how do i make an enemy move up, down, left and right.
I got the message on your tutorial saying use this for up and down:
onClipEvent(enterFrame){
this._xscale = _root.hp;
if(_root.hp
? ok thats weird it deleted some of my message, well what iw as gonna say is could you put the full code here and that be great thanks
To make the animations you just have to practice. You aren’t going to be good at it straight away.
Your code got cut off but you have a < sign. It gets taken out because of strip_tags function.
For up and down, this code will work:
onClipEvent(enterFrame){
if(this._y < (_root.man._y - 25)){
this._y = 3;
}
if(this._y < (_root.man._y 25)){
this._y -= 3;
}
}
For left and right try these tutorials:
http://www.awestyproductions.com/tutorials/flash-tutorials/basic-artificial-intelligence/
http://www.awestyproductions.c.....ce-part-2/
ok but where do i put that code in here:
onClipEvent(enterFrame){
tx = this._x;
mx = _root.man._x;
rx = tx - mx;
if(this.hitTest(_root.man)){
this.gotoAndStop(2);
}else{
if(rx >= 1 && rx-300){
this._x = 1;
}
this.gotoAndStop(1);
}
}
thanks
oh and about the animations, i can draw them fine but im wondering how to make them show the walking left picture when i make them move left and the walking right picture, and the jumping picture etc?
onClipEvent(enterFrame){
tx = this._x;
mx = _root.man._x;
rx = tx - mx;
if(this.hitTest(_root.man)){
this.gotoAndStop(2);
}else{
if(rx >= 1 && rx-300){
this._x = 1;
}
this.gotoAndStop(1);
}
if(this._y < (_root.man._y - 25)){
this._y = 3;
}
if(this._y < (_root.man._y 25)){
this._y -= 3;
}
For the animation, this should help:
http://www.awestyproductions.c.....ight-game/
}
thanx man thats helped me so much i have been looking evry where so u cant go through the wall thanx so much ur a legend
**Error** Scene=Scene 1, layer=Layer 3, frame=1:Line 1: Clip events are permitted only for movie clip instances
onClipEvent(enterFrame){
Total ActionScript Errors: 1 Reported Errors: 1
You need to put the code on a MC, not the frame.
you mean in the movie clip
Wow, i just realised i could use apply this tut to making a wall in my fight game. GREAT TUTS AND GREAT WEBSITE.
Tyvm
thanks it helped me out alot although i dont know why but when i play my maze game i made my person can go right through the walls can anyone help me out?
this really helped me out alot although for some reason when i play my game my person can go right through the walls can anyone help me?
ahhhhh
dude i like kool games gimme one now!!!
Tyus hlped meu sooooee muh. Tyhank yew alyot syr. eye luvvvv donut’s and iahve ai mustache
@Tamar: Make sure the instance names are right.
Thank you!
Now I finaly understand hitTest!!
just stop, settle down, calm down please
Awesome tutorial! I needed this soooooo much!!
i am slow
hi i was wondering how if u move an mc into a certain place that it will open a new screen???
Do you want it to launch another screen, seperate from the flash movie?
ooooh yes please
I actually dont know how to do that sorry. :\
k all gud
Great tutorial!!! Great Website!!!
I have a different type of movement system which I am using shown below but i am adding a wall in with I have drawn same method as the maze but I can’t get it to work in line tith what i have done hope you can help
if (Key.isDown(Key.UP)) {
xSpeed = thrust*xThrustPercent;
ySpeed = thrust*yThrustPercent;
flames._visible = 0;
} else {
xSpeed *= decay;
ySpeed *= decay;
flames._visible = 0;
thrustSound.stop();
soundStartable = true;
}
//
if (Key.isDown(Key.RIGHT)) {
_rotation = 10;
}
//
if (Key.isDown(Key.LEFT)) {
_rotation -= 10;
}
}
Can you please paste the whole code you were using?
Theres a fair chunk but here goes for the whole code
onClipEvent (load) {
// Start up
_root.explosion.stop()
_root.cell._visible = 0;
_root.ship._alpha = 0;
_root.amoeba._visible = 0;
_root.circle.ring._visible = 0;
_root.ship.body.body2._visible = 0;
_root.mask.label._visible = 0;
_root.texts.whiteText._visible = 0;
_root.startButton.blackButton._visible = 0;
_root.shipShadow._alpha = 0;
_root.circle.swapdepths(4000000);
_root.mask.swapdepths(4000001);
_root.texts.swapdepths(4000002);
_root.startButton.swapdepths(4000003);
radius = _root.ship.body._height/2;
radius2 = _root.circle._width/2;
centerx = _root.circle._x;
centery = _root.circle._y;
bonusLifeScore = 500;
score = 0;
lives = 0;
level = 2;
totalcells = 0;
cellNumber = 1;
explosionNumber = 1000000;
shotNumber = 1000;
totalShots = 1;
maxShots = 10;
shotSpeed = 20;
thrust = .6;
decay = .98;
maxSpeed = 8;
//
// initiate sounds
shotSound = new Sound(this);
shotSound.attachSound(”shoot”);
soundStartabe = true;
//
// functions (named accoding to what they do)
function initiate() {
_root.startButton.enabled = false;
_root.ship._rotation = 0;
if (started) {
buttonFadeOut = true;
}
for (i=cellNumber; i>0; i–) {
removeMovieClip(”_root.cell” i);
}
death(_root.amoeba);
score = 0;
level = 2;
lives = 7;
dead = false;
fadeIn = true;
}
soScores = SharedObject.getLocal(”flashcookie”);
mListener = new Object();
var score = 0;
if (soScores.data.highScore != null)
{
highScore = soScores.data.highScore;
}
Mouse.addListener(mListener);
mListener.onMouseDown = function()
{
score ;
if (score > highScore)
{
highScore = score;
soScores.data.highScore = highScore;
}
};
//
function wraparound(whichObject) {
with (whichObject) {
delta_x = _root.circle._x-_x;
delta_y = _root.circle._y-_y;
angle = Math.atan2(delta_y, delta_x);
distance = Math.sqrt((delta_x*delta_x) (delta_y*delta_y));
if (distance>radius2) {
_x = (Math.cos(angle)*(radius2)) centerx;
_y = (Math.sin(angle)*(radius2)) centery;
}
}
}
//
function collisions(whichObject) {
for (a=1; a90) {
whichObject.hit(0);
}
death(_root.ship);
}
}
for (b=1000; b90) {
explosion(whichShip);
}
if (whichShip == _root.ship) {
deathTime = getTimer();
if (!dead) {
lives–;
}
dead = true;
thrustSound.stop();
soundStartable = true;
whichShip._alpha = 0;
_root.shipShadow._alpha = 0;
whichShip._x = 302;
whichShip._y = 295;
} else {
whichship.appearTime = getTimer();
whichship.pauseTime = (Math.random()*7000) 7000;
whichship._visible = 0;
}
}
//
// toggle buttons
_root.mask.backgroundButton.onRelease = function() {
_root.circle.drop._visible = !_root.circle.drop._visible;
_root.circle.ring._visible = !_root.circle.ring._visible;
_root.mask.dropShadow._visible = !_root.mask.dropShadow._visible;
_root.background._visible = !_root.background._visible;
_root.ship.body.body2._visible = !_root.ship.body.body2._visible;
_root.mask.label._visible = !_root.mask.label._visible;
_root.mask.maskedArea._visible = !_root.mask.maskedArea._visible;
_root.texts.whiteText._visible = !_root.texts.whiteText._visible;
_root.startButton.blackButton._visible = !_root.startButton.blackButton._visible;
_root.startButton.colorButton._visible = !_root.startButton.colorButton._visible;
};
//
_root.mask.qualityButton.onRelease = function() {
toggleHighQuality();
};
}
//
onClipEvent (enterFrame) {
_root.startButton.onRelease = initiate;
// Adds new cells when all die and extra
if (totalcells == 0 && started) {
level ;
cellNumber = 1;
}
if (cellNumber0) {
pauseTime = getTimer()-deathTime;
if (pauseTime>2000 && lives>0) {
dead = false;
fadeIn = true;
}
}
//
// extra lifes
if (score>bonusLifeScore) {
bonusLifeScore = 500;
lives = 1;
}
//
// Button fading
with (_root.startButton) {
if (lives == 0) {
_root.startButton.enabled = true;
if (_alpha0 && buttonFadeOut) {
_alpha -= 10;
} else {
buttonFadeOut = false;
}
}
//
// map loop
wrapAround(_root.ship);
//
// Shotting
if (Key.isDown(Key.SPACE)) {
if (!spacekeydown && !dead && totalshotsmaxshots) {
shotnumber = 1000;
}
shotyspeed = shotspeed*yThrustPercent;
shotxspeed = shotspeed*xThrustPercent;
spacekeydown = true;
}
} else {
spacekeydown = false;
}
//
with (_root.ship) {
// Fading in
if (_alphamaxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}
//
// move ship based on calculations above if ship is visible
_y -= ySpeed;
_x = xSpeed;
if (dead) {
xSpeed = 0;
ySpeed = 0;
}
//
// collision points
angle1 = (_rotation-90)*(Math.PI/180);
tipy1 = (Math.sin(angle1)*radius) _y;
tipx1 = (Math.cos(angle1)*radius) _x;
angle2 = (_rotation 57)*(Math.PI/180);
tipy2 = (Math.sin(angle2)*radius) _y;
tipx2 = (Math.cos(angle2)*radius) _x;
angle3 = (_rotation-237)*(Math.PI/180);
tipy3 = (Math.sin(angle3)*radius) _y;
tipx3 = (Math.cos(angle3)*radius) _x;
_root.shipShadow._x = _x 4;
_root.shipShadow._y = _y 4;
_root.shipShadow._rotation = _rotation;
}
}
Fuck…
lol im not going to be able to do that without flash, which I dont have at the moment because my computer crashed.
Actually just email it to:
awestyproductions@gmail.com
And I will try and fix it as soon as I has access to flash. (I do have a copy of flash, but it is on a laptop with 64mb of RAM :\).
[…] That is so ‘man’ can’t walk over the other guy. For an explanation on that code take this tutorial: http://www.awestyproductions.c.....ts-part-2/ […]
OMG!! Awesty used a cuss word NOOOO! What has this world come to?!???!!? Nah, just joking
@Bryce K: I swear all the time
Hello,
Thanks for your wonderful MAZE tutorial. One problem though. I am loading the maze .swf file into another .swf using and empty movie clip to load the .swf into. When I test the maze in the parent .swf file, my little “man” in the maze walks straight through the walls. It works fine when I test the maze.swf alone.
Any help would be greatly appreciated!
Thanks — Jenny
Hi Awesty
Your tutorials are dam good and I’m really starting to get the hang of flash. Your explanations of the coding are very detailed and relatively easy to understand.
so thanks
Firebelly
P.S if your Wii’s online can I have your friend code
@Jenny I’m not sure I understood what you mean, but I believe maybe some of your _root.man commands may have to change a bit. What I mean is that now you’re man isn’t in the root anymore, he is inside an “empty movie clip” as you said, so it would e something like _root.mymc.man hitTest etc…
I’m not sure tho.
Hope this helps.
Hi Grifo,
Thanks for trying. Well, I tried that. I tried just about everything, but when I test the “parent” swf, the man walks straight through the walls, not matter what. Tried to put the _lockroot command in the main .swf, the script on the man, even on the load button, but nothing worked. Thanks again.
Jenny
@Jenny: Is the maze game coded in the parent .swf, or the child .swf?
@FireBelly: I havented got it online yet, I am havings problems with it trying to update. It goes extremely slow.
try going onto the nintendo wifi forums for help - I had to install a patch for my ninty wifi usb thing. It’d be cool to see your mii and play you online when wii games eventually go online. Woo smash bros brawl