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 cant 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 wouldnt 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 doesnt 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 didnt 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 didnt 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");
}
Whats this, I asked myself, a comma and hittest with a lower case t, this couldnt 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 cant 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 CANT 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, Ive 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
doesnt 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 |
| » Author |
| An ex network admin who found solace in lines of code. |
| » Download |
| Download the files used in this tutorial. |
| Download (42 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


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