Internet Commerce

Partners & Affiliates

Developer Channel


Featured Flash FLA
Gallery Downloads 11401 Flash Movies | 5 New Flash Movies Added
What's New | Top 100

Featured FLA

»  Author: Nick Kouvaris
»  Title: Znax
»  Description: Znax is a board game. Click 4 tiles of the same color and form squares as big as you can. You will erase all the tiles inside the square and collect points. Get maximum score if you make a square with game edges.
»  More by: Nick Kouvaris


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 4941 Flash Sites | 1 New Flash Links
What's New | Top 100 Flash Site

Featured Site

»  Author Agence WOP Digital Agency
»  Title: Electricdrum
»  Description: French WOP Agency, 3D websites, Flash (Papervision, Away 3D), event or institutional projects. The agency operates on all digital projects: consulting, design, graphic design, development, online communication. The WOP agency follows you on the implementation of original, creative and optimized digital projects.


Random Links | Add your own Flash Related Links
Flash Tutorials 1481 Tutorials 7 New Tutorials Added!
What's New | Top100

» How To Make A Simple Animation Using Christmas Clips
» Simple Step by step flash game tutorial Spot the diffrence
» How To Make A Moving Text Slide
» Create Flash Banner With Text Float Effect
» How To Make Zoo Photos Slideshow
» How To Make A Dolphin Photos Slideshow
» How To Make A Fathers Day Slideshow
» How To Make A Transparent Background of Your Flash File
» Create Flash Banner With Text Disco Light Effect Today we will introduce you a Text Disco Light eff
» Unknown Tag: Title10
Random Tutorial | Add Site


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Actionscripting

Categories Advanced HitTest in Flash MX
Author: Art Bowles | Website: www.launchfire.com |

 
Page 2
«prev 1 2 3 next»

Problems

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");
//ORIGNIAL MX

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.

YOU CAN’T USE A RASTER IMAGE, YOU HAVE TO USE A VECTOR.

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");
}

«prev 1 2 3 next»

» Level Intermediate

Added: : 2003-01-14
Rating: 6.54 Votes: 28
Hits: 15
» Author
An ex network admin who found solace in lines of code.
» Download
Download the files used in this tutorial.
Download (42 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments