Hi! Thanks for visiting my blog. If you've received any value from my content would you mind supporting my new startup by downloading our browser add-on? It's called PriceBlink and makes online shopping a breeze. You can watch it in action here and download it for Chrome, Firefox, IE, or Safari by going to PriceBlink.com. Thank you and I hope you enjoy!

Typecasting and Movieclip Visibility in ActionScript 2.0

Jul 28

This is just something basic I learned today when typecasting variables as movieclip objects. Prior to AS2.0 I always made references to movieclip visiblity using the following statements:

my_mc._visible = 0;
my_mc._visible = 1;

If you typecast your variable like:

// Reference to a datagrid instance on the stage
var my_mc:MovieClip = my_dg;

and try to set the visibility of the component using:

my_mc._visible = 0;
my_mc._visible =1;

You’ll get the following error:

“Type mismatch in assignment statement: found Number where Boolean is required.”

The compiler sees these values as integers or numbers instead of booleans. You have one of two options, the first which makes more sense:

my_mc._visible = false;
my_mc._visible = true;

or cast your number to Boolean:

my_mc._visible = Boolean(0);
my_mc._visible = Boolean(1);

This obviously isn’t a big issue but it’s nice to see typecasting at work and there are many other places where this will come in handy.