UMich_2017
UMich_2017

Reputation: 131

Setting Class from External SWF - Implicit Coercion

I'm trying to load in an SWF into my main file. The SWF is ColorWheel.swf, and the main file is c_test.as. Here is my code:

var loader:Loader = new Loader();   
this.addChild(loader);
loader.load(new URLRequest("ColorWheel.swf"));  
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
var myClass:Class;

function imageLoaded(e:Event):Class
{
   loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,imageLoaded);    
   return loader.contentLoaderInfo.applicationDomain.getDefinition("StageColorWheel") as Class;
}

myClass = imageLoaded;
var myWheel:BitmapData = new myClass();

This gives me an implicit coercion error where I have myClass = imageLoaded. I'm fairly certain this is an easy fix, but if anyone has an answer I'd much appreciate it. This was my current code before (which worked, but everything following loading the swf into c_test.as was included in the imageLoaded function.

var loader:Loader = new Loader();   
this.addChild(loader);
loader.load(new URLRequest("ColorWheel.swf"));  
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
var myClass:Class;

function imageLoaded(e:Event):Class
{
   loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,imageLoaded);    
   myClass = loader.contentLoaderInfo.applicationDomain.getDefinition("StageColorWheel") as Class;

   var myWheel:BitmapData = new myClass();
   //rest of code - works
}

Upvotes: 0

Views: 28

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

To cover the why of the actual error, in the following line:

myClass = imageLoaded;

You are not calling the function because you're missing the parenthesis (). It should be:

myClass = imageLoaded();

Without the parenthesis (), you're just assigning a reference to the function itself to the myClass var, which is what is causing the error because the var needs to hold a Class reference not a Function reference.

However, even if you correct that, this is not going to work right.

The imageLoaded() method should only run after the load completes (and indeed it will run when it completes because you have it as an event handler as well).

At the time you do myClass = imageLoaded(), the loader has not completed loading yet so your result will be different/null.

Your second way of doing it is the appropriate way to go about this.

Upvotes: 1

Related Questions