First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Art Bowles | Website: http://www.launchfire.com


Most of the problems I ran into were a result of testing for hits by running through a loop.

for (i=1; i<8; i++) {

eval("hit"+i) =_root.TestGoalie.hitTest("_root.ActionPuck");//ORIGNIAL F5 using target
if (hit1)
trace ("hit");

In MX, you can’t use eval on the left side of the assignment operator (=), you have to use []. After some experimentation, I got the following to work.

this["hit" + i] = _root.TestGoalie.hitTest("_root.ActionPuck");

Next, I had to test against multiple goalies. My original code used eval a lot, of course this wouldn’t work.

eval("hit"+i) = eval("_root.TestGoalie" +i).hitTest(_root.ActionPuck);
// F5 version

So this time I was going to use [] on the left side, and it worked. this["hit" + i] = eval("_root.TestGoalie" +i).hitTest(_root.ActionPuck);
// MX target, works


In my research I saw that many people were using [] instead of eval on the right side of the assignment operator, so naturally, I wanted to try it out. After a few attempts I got the following to work. Notice the left side [ replaces the period.

this["hit" + i] = _root["TestGoalie" +i].hitTest(_root.ActionPuck);
// MX target, also works

This however doesn’t work.

this["hit" + i] = ["_root.TestGoalie" +i].hitTest(_root.ActionPuck);// DOENST WORK

I was just about finished testing using Target when I realized I forgot about testing for multiple targets on the same variable. For instance:

hit1 = eval("_root.TestGoalie" +i).hitTest(_root.ActionPuck);
hit1 = eval("_root.TestGoalie" +i).hitTest(_root.ActionPuck);

At first it didn’t work, then I realized, my conditional statement that checks if hit1 was true, was outside my loop. Moving the conditional statement inside the loop worked. However, in tracking down the problem (meaning I didn’t look at the most obvious problem first) I ran across this example in the MX help file.

if(_root.ball, hittest(_root.square)){
trace("ball intersects square");

What’s this, I asked myself, a comma and hittest with a lower case t, this couldn’t be right, so I tried it out.

hit1 = eval("_root.TestGoalie" +i), hittest(_root.ActionPuck); //with comma

At first, it looked like it worked, but then I realized that everytime I shot the puck, regardless of what the puck hit, a hit was registered. As it turned out, you can use the lower case hittest, but you can’t use a comma. Want proof, try these out

//hit1 = eval("_root.TestGoalie" +i), hittest(_root.ActionPuck);// with comma DOESN'T WORK
//hit1 = eval("_root.TestGoalie" +i), hitTest(_root.ActionPuck);// with comma DOESN'T WORK
//hit1 = eval("_root.TestGoalie" +i), nothing(_root.ActionPuck);// with dummy word DOESN'T WORK
//hit1 = eval("_root.TestGoalie" +i). hittest(_root.ActionPuck);// with period WORKS ????
//hit1 = eval("_root.TestGoalie" +i). nothing(_root.ActionPuck);// with period DOESN'T WORK

Testing using the Target Argument

As I said earlier, this game was going to test hits against round targets and goalie figures, so I was going to have to use the shapeflag argument, as this would test if the puck hit the actual shape, not just the whole movie clip. Thus if the puck went between the goalies legs, it would not hit the shape of the goalie. However, on my first few attempts, shooting between the legs still registered as a hit (block). At first I was perplexed but the answer was staring me in the face.


When you import a jpg or png graphic into flash, it is actually a square graphic, even if, in the case of the PNG, you elected to make part of it transparent. Click on a graphic in flash, there will be a square surrounding it. However, if you draw in flash, or import in a vector graphic, clicking on it will highlight only its actual shape. Now, I’ve always had problems importing .eps or .ai illustrations into flash, so this time I decided to use my brand new Freehand 10 program. I imported my illustration (.ai) into freehand, then saved it as a .fh file, and imported it into flash. This worked brilliantly.

Again, testing using the shapeflag argument, on a basic level works the same in F5 as in MX.

// F5 and MX compatible

hit1 = _root.TestGoalie.hitTest(_root.ActionPuck._x, _root.ActionPuck._y, true);
if (hit1)
trace ("hit1");
if ( _root.TestGoalie.hitTest(_root.ActionPuck._x, _root.ActionPuck._y, true))
trace ("hit1 if");

The following all work depending on what your trying to do // F5 version that doesn’t work
eval("hit"+i) = eval("_root.TestGoalie" +i).hitTest(_root.ActionPuck._x,
_root.ActionPuck._y, true);

// MX version
this["hit" + i] = _root.TestGoalie.hitTest(_root.ActionPuck._x, _root.ActionPuck._y,true);

//Change goalie instance name to TestGoalie1

this["hit" + i] = eval("_root.TestGoalie" +i).hitTest(_root.ActionPuck._x,
_root.ActionPuck._y, true);

this["hit" + i] = _root["TestGoalie" +i].hitTest(_root.ActionPuck._x,
_root.ActionPuck._y, true);

So now when you shoot the puck, if it goes between his legs, it does not register as a hit.
'' Using either method in a conditional statement

You have probably noticed that except for the basic level examples, all my examples create variables, then test if the variable is true using a conditional statement

for (i=1; i<8; i++) {
this["hit" + i] = _root["TestGoalie" +i].hitTest(_root.ActionPuck._x,
_root.ActionPuck._y, true);
if (this["hit" + i])
trace ("hit");

» Level Intermediate

Added: 2003-01-14
Rating: 7 Votes: 28
(10 being the highest)
» Author
An ex network admin who found solace in lines of code.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.