Actionscript 3.0 type downcast issue

470 Views Asked by At

I have implemented a new class that extends MovieClip. It's name is base.MovieClipWithDelays ("base" here is a package name).

My scene contains such an object named Blah. In Symbol Properties I checked Export for ActionScript and Export in first frame checkboxes. I set Class Name as T_Idle_0. And I specified it's Base class as base.MovieClipWithDelays.

The problem is that following code leads to the Type Error:

var dob:DisplayObject = getChild("Blah");
trace("SuperClass = " + getQualifiedSuperclassName(dob));
return MovieClipWithDelays(dob);

it outputs:

SuperClass = base::MovieClipWithDelays
TypeError: Error #1034: Type Coercion failed: cannot convert T_Idle_0@1ec59e9 to base.MovieClipWithDelays.

As you can see, it's superclass name is OK. Nevertheless, it fails to downcast it. How is it possible and how do I workaround it?

1

There are 1 best solutions below

3
On

You cannot set the base class of a library MovieClip to a custom class. You can either set it to Sprite or MovieClip. To do what you want to do, you have two solutions:

1 . Manage everything (drawing, etc.) from your MovieClipWithDelays class. i.e. don't make it depend on a library object.

Or:

2 . Make your MovieClipWithDelays wraps a MovieClip instance.

var libraryMC:MovieClip = new SomeLibraryMovieClip();
var customMc:MovieClipWithDelays = new MovieClipWithDelays(libraryMC);

Then within MovieClipWithDelays, you'll need to have some function and properties to handle the wrapped MovieClip.