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.


aaron says: Monday, November 10, 2008

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: Sunday, September 07, 2008

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: Saturday, July 19, 2008

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: Wednesday, June 25, 2008

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: Wednesday, June 25, 2008

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: Wednesday, June 25, 2008

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: Thursday, April 10, 2008

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


Hank says: Wednesday, March 05, 2008

im getting no luck with this tutorial.

1/10 thanks anyway for help


Admin Bob says: Sunday, March 02, 2008

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: Sunday, March 02, 2008

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.

  Next 


 

 

 

 6

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact