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.

hittestmethod1.gif

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.

hittestmethod2.gif

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.

hittestmethod3.gif

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.

hittestmethod4.gif

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.

11 Responses to “Interactivity in Flash using hitTest() method”
  1. suresh says:

    I have done

  2. xteleen says:

    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?

  3. MYip says:

    I don’t think so. You have to revise your looks. In general your blog is good, but sometimes your posts are creepy.

  4. ibor says:

    thnx, bro!

  5. sam the asian kid who sits next to me says:

    umm………. if you could read the original text is not the spaceship is on the planet
    it says The spaceship is in space!

  6. onlineflash says:

    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?

  7. brian says:

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

  8. spoonman says:

    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

  9. spoonman says:

    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(){

  10. Akhlaq Ghouri says:

    I am really impressed by u and found what i wanted to. Its a good job specially for biggners and new comers.
    Thanks.

  11. shashi says:

    wow i have done it ,
    i fased some problem finaly i got through,
    thnks

Leave a Reply