PDA

View Full Version : hitTest() returns undefined with a "Button"


deekay3k
11-29-2005, 09:21 PM
OK, forgive me if this has been covered before, but this is really peculiar.

There seems to be a discrepancy when tracking a hitTest (with the mouse) if the object you're testing is set as a "Button" in your properties dropdown. Works just fine with Movie Clips, an example:

trace( theMC.hitTest(_xmouse, _ymouse, false) );
will return the proper true or false

trace( theButton.hitTest(_xmouse, _ymouse, false) );
returns undefined

The only difference between those clips is their property Type. Has anyone determined a reliable workaround? This is really bizzare. :confused:

amen0
11-29-2005, 09:27 PM
for the btns there is no method hitTest for them
but u can use rollOver event..........

Scottae
11-29-2005, 09:34 PM
It's not bizarre at all.......hitTest (http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002475.html) is a method of the MovieClip (http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002436.html) class.

If you were to actually type the button as a Button, and then try to call hitTest from this button, compiling would give you errors:

var my_btn:Button;
my_btn.onRelease = function ()
{
trace (my_btn.hitTest (_xmouse, _ymouse));
};

**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 4: There is no method with the name 'hitTest'.
trace (my_btn.hitTest (_xmouse, _ymouse));

Total ActionScript Errors: 1 Reported Errors: 1

deekay3k
11-29-2005, 09:35 PM
Certainly, I could use onRollOver/onRollOut. Suppose I already have them applied to a button and don't want to/can't overwrite them. Seems like there should be another way, but then, this wouldn't be the first time the MM guys let me down with a big oversight.

deekay3k
11-29-2005, 09:38 PM
If you were to actually type the button as a Button, and then try to call hitTest from this button, compiling would give you errors:
Which would explain why I didn't get an error, my buttons were not created and typed w/ AS.

Anyone have any other method of tracking collision between a Button and your mouse?

cancerinform
11-29-2005, 09:40 PM
I have never tried hittest with the button component but it should work since the button component is a movieclip, if you need to use something similar to a button.

Scottae
11-29-2005, 09:45 PM
I have never tried hittest with the button component but it should work since the button component is a movieclip, if you need to use something similar to a button.

Uhmmm......no it is not. Button (http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001984.html) is not the same as a MovieClip (http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002436.html).

trace (Button == MovieClip); // false
trace (Button instanceof MovieClip); // false

// I created a button instance on stage called my_btn
// can't dynamically create buttons
trace (typeof my_btn); // object

// Dynamically create movieclip
_root.createEmptyMovieClip("my_mc", 1);

trace (typeof my_mc); // movieclip

If you look at the links I provided, you will see that both Button and MovieClip extend Object (http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001984.html) which is the root of the ActionScript class hierarchy.

Scottae
11-29-2005, 09:53 PM
@ cancerinform - I just realized you are talking about Button component. I don't think that is what they are talking about. I believe they are talking about a regular Button (http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001984.html)

cancerinform
11-29-2005, 10:02 PM
I know but in case you build a wall :rolleyes: with buttons for example :D , which ought to be hit by gunfire.

Just for the sake of being complete, another method which does not work for buttons only for movieclips is the getURL() method when used in external class files.

deekay3k
11-30-2005, 01:28 PM
Ah well, looks like I'm stuck with those limited Button methods. Thanks to everyone who replied.