A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11336 Flash Movies | 2 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Surjit Dhami
» Title: Book
» Description: Book
» More by Surjit Dhami


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

Featured Site

» Posted in the Flash Kit Links section
» Title: All-American Rejects
» Description: Get to know this great band by exploring their "practice room".


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

» Create xml slideshow with free template
» How to Insert a Multilingual Subtitle Into Your Flash Video Studio
» How to Create Cool Halloween Slideshow
» Debugging flash using the Firebug console
» Create Flash Slideshow on Blogger
» FLASH TRICKS IN WEB ADVERTISING: FLASH BANNERS
» HTML Photo Gallery Tutorial
» Create your first flash site – PART 1
» How to Make a Flash Photo Gallery
» Unknown Tag: Title10
Random Tutorial | Add Site

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume


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.62 Votes: 26
Hits: 11
» 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
 
   
 

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs