Explode Enemy with hitTest

Check to see if a laser is hitting the enemy

Now we'll use the hitTest method to check if a laser is coming into contact with the enemy. The hitTest will return one of two values: true or false (either the two instances are touching, or they aren't). Start an if statement and use the hitTest for the condition:

How can we tell if that works? We can quickly and easily test this code with the trace function. Trace is specific to the testing environment (your final user will never see what trace is doing). We can use trace to return the value of a variable if we think there might be a problem with our code, or trace can just return a string. That's what we'll do here. If our conditional statement is true (if laser is hitting enemy), we'll have trace return the string "hit":

Test your project now. Whenever a laser is contacting the enemy, an Output window should pop up and display the word "hit".

Play the screen capture movie below to see the Output window working properly. Your own Output window will probably pop up in a different place on your screen, which is absolutely fine:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player