RSS link icon

.

Hit test in actionscript 3


In this Flash tutorial you will learn to use actionscript 3 to make a simple hit test, one of the most used functions and defenently needed if you want to make a flash game.

First off we will need to make two things, and object to move, in this case a ball, then the ground, so with the oval tool draw a ball, right click it and convert it to a movie clip, go to the properties panel and give it an instance name as shown below. I named mine ball.

hit test actionscript 3 tutorial

hit test actionscript 3 tutorial

Now with the rectangle tool make the ground and place it under our ball, right click and convert it to a movie clip, give it an instance name, I called mine ground.

hit test actionscript 3 tutorial

Now the last thing we need so to hit F9 and type in our actionscript code, the lines with // infront of them is comments explaining the code, thats all the grey text, I have tried to explain it as simple as possible.

//Because this is AS3 everything needs to be called through
//eventlisteners, so this first lin calls and listens for the ball
ball.addEventListener(Event.ENTER_FRAME, enter_frame);
//here is our function, it is called at the beginning of the movie
function enter_frame(event:Event):void{
    //An if statement, saying if the ball touches the ground,
    //then just make a trace of text.
    if (ball.hitTestObject(ground)) {
    	trace("ball is on the ground")
    } else {
    	ball.y += 10
}
}

Now test your movie, if the ball hits the ground it should stop.


Ari says: 2010-04-11

The second-to-last '}' needs to be indented. That's why everyone is having a problem.

aaron says: 2008-11-10

I am attempting to make a file in which a ball is controlled by the keyboard and goes through a maze of sorts. This tutorial did not help me, the ball still passes through the walls! that makes the maze no fun

Kaylan says: 2008-09-07

Does anyone know how to use the hit test using class file(.as) I have build up two class file for two different things to randomly move around the stage. I was wondering if anyone know how to combine these two classes using hitTestObject, where should two of them collide with each other it should explode. I tried to put the script in the one of those class file but nothing happen. I tried in the .fla file too and nothing happen as well. SO I am not sure what I should do. Has anyone experience this before and know what to doPlease help, thank you.

www.moremoney.tk says: 2008-07-19

Excellent tutorial!, i am too making a simple maze game to further master the "hit test" i have been looking for a simple explanation on using "hit test" now i have found it, i am overwhelmed by how easy it is! thankyou very much!! /Dici

Bee says: 2008-06-25

Thank you very much! i have been looking over the internet for a while to find something to explain hittest and all of them were too complex but this was simple enough so i could get it. thanks! oh and it may be annoying but i think you learn better when you have to type out the code yourself instead of just using copypasta

Bee says: 2008-06-25

Thank you very much! i have been looking over the internet for a while to find something to explain hittest and all of them were too complex but this was simple enough so i could get it. thanks! oh and it may be annoying but i think you learn better when you have to type out the code yourself instead of just using copypasta

Bee says: 2008-06-25

Thank you very much! i have been looking over the internet for a while to find something to explain hittest and all of them were too complex but this was simple enough so i could get it. thanks! oh and it may be annoying but i think you learn better when you have to type out the code yourself instead of just using copypasta

Andrew says: 2008-04-10

Thanks for this tutorial, actionscript 3 is slowly making more sence.

Hank says: 2008-03-05

im getting no luck with this tutorial. 1/10 thanks anyway for help

Admin Bob says: 2008-03-02

hi 1031, the ground is a predefined object named ground. But I am working on a game where I implement x and y koordinates instead, so if you follow my blog once in a while you will see an indept tutorial when I finish the game.

1031 says: 2008-03-02

Nice tutorial Bob! Can you explain about the parameter (ground) that is passing into the hitTestObject function Is it a variable or a predefined object Thanks.

Admin Bob says: 2008-02-27

hi swallowprincess, well I dont know which error you, try to tell me :-)

Burned says: 2008-02-26

Lu, why donChr(34)t you quit being lazy and write the tutorials yourself if your going to complain. IChr(34)m sure it wasnChr(34)t easy for him to do his first time either.. Nice tutorial..

swallowprincess says: 2008-02-25

how come there are so many errors i pressed enter and all these errors came up. and i couldnChr(34)t do it successfully. :(

zeshan says: 2008-02-13

this was good and You know, it would be better if we had the code to just copy and paste, rather than spend ages trying to type it ourselves. It took me ages, then when I pressed enter to view it a load of errors came up

Vlad says: 2008-01-30

Does this really work. Does it differentiate between real hits and if the "boxes" of the objects are touching. Say for example that I have to circles, will it be able to say if the circles are touching or will it just test for the boxes of the circles

Admin bob says: 2008-01-27

Yes sorry Lu, We will try to type in the code so you can copy past in in the future.

Lu says: 2008-01-25

You know, it would be better if we had the code to just copy and paste, rather than spend ages trying to type it ourselves. It took me ages, then when I pressed enter to view it a load of errors came up. Just trying to be helpful