hitTest() method is very useful for flash developers especially when creating games or elearning applications. In this tutorial I will give a simple example on how to detect collision between two movie clips using hitTest() method.
STEPS TO FOLLOW:
1. Create a new Flash document and save it as hitTest.fla on your computer. Go to Modify >> Document (Ctrl+J) and set width to 400 pixels and height to 300 pixels.
2. Insert three new layers and rename them from top to bottom: actions, spaceship, planet and background.
3. In the first frame of the background layer import what picture you desire: go to File >> Import >> Import to Stage and select the picture from your computer and click the Open button. Align the picture both horizontal and vertical.
4. Go to the planet layer and in the first frame draw or import a picture of a planet. Select the planet picture or drawing and convert it to a movie clip: go to Modify >> Convert to Symbol (F8) and from the window that appears select MovieClip as Type, center for registration point and give it a name of planetMC, then click Ok button. Select the movie clip, go to the Properties Inspector and type planet_mc in the Instance Name text field.
5. Now, in the first frame of the spaceship layer draw or or import a picture with a spaceship. Convert it to a movie clip with the name spaceshipMC. Give it also an instance name of spaceship_mc.
6. Go back to the background layer and put here a dynamic text field (use Text tool from the Tools panel). Choose Comic Sans MS font, size 14 and white color. Give to the text field an instance name of space_txt.
7. Go to the first frame of the actions layer, open the Actions panel (F9) and write the ActionScript code below:
spaceship_mc.onPress = function(){
startDrag(this, false, 40,15, 360, 285)
}spaceship_mc.onRelease = spaceship_mc.onReleaseOutside = function(){
this.stopDrag();
if (spaceship_mc.hitTest(planet_mc)){
space_txt.text="The spaceship is on the planet!"
}else{
space_txt.text="The spaceship is back in space!"
}
}
8. Great! Now test your movie (Ctrl+Enter) and you’ll see that space_txt change the text displayed depending on spaceship_mc overlaps or not planet_mc.





Entries (RSS)
March 19th, 2008 at 5:09 am
I have done
April 6th, 2008 at 6:26 pm
How about hit test using this scenario:
I have 3 boxes
Blue, Green & Yellow
Green & Yellow boxes are movie clips and is inside a movie clip named 2BoxesInside_mc
Blue is a movie clip also but outside 2BoxesInside_mc.
how can i perform a hit test to Green & Yellow boxes when i drag Blue Box?
Is it possible for a hit test to be performed?If not, is there any other way to do it?
April 9th, 2008 at 3:10 am
I don’t think so. You have to revise your looks. In general your blog is good, but sometimes your posts are creepy.
April 11th, 2008 at 7:44 am
thnx, bro!
April 11th, 2008 at 10:26 am
umm………. if you could read the original text is not the spaceship is on the planet
it says The spaceship is in space!
April 13th, 2008 at 12:45 pm
I did all it says but when i come to testing it it says:
**Error** Scene=Scene 1, layer=actions, frame=1:Line 3: Syntax error.
}spaceship_mc.onRelease = spaceship_mc.onReleaseOutside = function(){
Total ActionScript Errors: 1 Reported Errors: 1
any ideas?
April 14th, 2008 at 8:35 am
some of your code was misplaced, it doesnt read right on flash MX, but this does
spaceship_mc.onPress = function(){
startDrag(this, false, 40,15, 360, 285)
}
spaceship_mc.onRelease = spaceship_mc.onReleaseOutside = function()
{
this.stopDrag();
if (spaceship_mc.hitTest(planet_mc)){
space_txt.text=”The spaceship is on the planet!”
}else{
space_txt.text=”The spaceship is back in space!”
}
}
April 20th, 2008 at 5:27 pm
I get this error when test the code what is grong?
**Error** Scene=Scene 1, layer=actions, frame=1:Line 3: Syntax error.
}spaceship_mc.onRelease = spaceship_mc.onReleaseOutside = function(){
Total ActionScript Errors: 1 Reported Errors: 1
April 20th, 2008 at 5:52 pm
i solve the problem jiji
good:
startDrag(this, false, 40,15, 360, 285)}
spaceship_mc.onRelease = spaceship_mc.onReleaseOutside = function(){
bad:
startDrag(this, false, 40,15, 360, 285)
}spaceship_mc.onRelease = spaceship_mc.onReleaseOutside = function(){
April 28th, 2008 at 6:19 am
I am really impressed by u and found what i wanted to. Its a good job specially for biggners and new comers.
Thanks.