Typecasting and Movieclip Visibility in ActionScript 2.0



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.



Comments are closed.