Custom Class Casting in Flash
Dr. B. | Friday, June 30, 2006
While developing a Flash 8 project using ActionScript 2 classes, I recently created a Tile class that extended MovieClip. The Tile is a custom scrolling background element that tiles itself for a seamless, endless horizontal scrolling.
I created a couple variables, typed as Tile, in my AppState class, the class I use to keep the current state of the Flash application. They are like so:
Now, since we're not yet using ActionScript 3, the only way to place new Tile instances onto the stage is with a call to
Why does it fail? Well, the compiler says this:
So, I thought to myself, I wonder if you can cast objects like you can with the
I'm looking forward to ActionScript 3, where we will be able to instantiate objects to be placed on the stage like you can for any other object:
But until then, I'll use this casting method. It's not rocket science, but I thought it was cool nonetheless.
Doug Smith, Senior Developer, Barefoot
I created a couple variables, typed as Tile, in my AppState class, the class I use to keep the current state of the Flash application. They are like so:
// Background movie clip tile references
var tile1:Tile = null;
var tile2:Tile = null;
Now, since we're not yet using ActionScript 3, the only way to place new Tile instances onto the stage is with a call to
attachMovie()
. However, the following call won't compile in AS2:
app.tile1 = _root.attachMovie(bgndMcName, "bgnd1_mc", app.depth++);
Why does it fail? Well, the compiler says this:
Type mismatch in assignment statement: found MovieClip where com.barefoot.ui.comp.Tile is required.
So, I thought to myself, I wonder if you can cast objects like you can with the
String()
and Number()
functions? It turns out you can! This works:
app.tile1 = Tile(_root.attachMovie(bgndMcName, "bgnd1_mc", app.depth++));
I'm looking forward to ActionScript 3, where we will be able to instantiate objects to be placed on the stage like you can for any other object:
app.tile1 = new Tile();
addChild(app.tile1);
But until then, I'll use this casting method. It's not rocket science, but I thought it was cool nonetheless.
Doug Smith, Senior Developer, Barefoot
0 comments
Post a Comment
« Home