Saturday, February 26, 2011

Getting Object Type On Mouse Event in AS3

Found this interesting. I've gotten used to checking object type, as run time. So, when I tried to figure out how to do it in AS3, I was surprised at some fairly convoluted approaches out there. Happily, the AS3 documentation came through, and I tried this out. Works like a charm, for my purposes.

function mouseOverListener(event:MouseEvent):void {

var targ = event.target;
trace("this: " + targ); // output is "[object Infopop]"
trace("type: " + targ.constructor); // output is "[class Infopop]"


if(targ.constructor == InfoPop){
trace("infopop");
}
}

For type checking, I want the constructor, not the object.

Update: Read the first comment on this post: very helpful!

5 comments:

sebestenyb said...

How about this:

if( event.target is InfoPop )

Cheers, Balazs

Kristin Henry said...

if(event.target is InfoPop){
--> works
if(event.target.constructor is InfoPop){

--> doesn't work


if(event.target.constructor == InfoPop){

--> works

Interesting. Nice to have options.

Kristin Henry said...

Honestly, there is endless fascination in code. So many ways to accomplish the same goal. I love it!

rP said...
This comment has been removed by the author.
rP said...

and from a performance point of view, which syntax to choose?