1
votes

I have a draggable movieclip on my stage. When it hits another object/movieclip the word: "hit" appears. So far, so good. But, I want the draggable movieclip to hit multiple other objects with each an different reaction (like loading an other movieclip or something).

This is the code I have for the hittest part:

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(blok3.hitTestObject(schaap))

       {
           output_txt.text = "HIT"
       }
       else
       {
           output_txt.text = ""
       }

}

SCHAAP is the draggable object, blok3 is the object that trigger the word "hit" when the draggable object hits it.

I hope you guys can help me out!

1
I'm going to suggest using switch/case instead of if else as all those if statements will be much harder on your processor than a switch/case statement, but that's just my opinion - Ryan Hollingsworth

1 Answers

0
votes

You already have a trigger and an output for one collision (if blok3 htis schaap, output "HIT") so a simple extension would be to add another if statement below the existing:

function handleCollision( e:Event ):void
{
    if(blok3.hitTestObject(schaap))    
    {
        output_txt.text = "HIT"
    }
    // else if statement to check for collision on second object
    else if(differentBlok.hitTestObject(schaap))
    {
        output_txt.text = "SMASH"
    }
    else
    {
        output_txt.text = ""         
    }

}

From here you can look at storing objects in arrays and looping through the array to check for a collision, rather than having a separate if statement for every one.