AS3 Creating a Class from an External SWF

I talked with my friend about this, he is convinced that you can do it and says that he did it, but I can’t get it to work.

I wonder if this is even possible. I tried typing var as a class that is inside the SWF loaded from outside, and then make an instance, but cannot do it.

some code

private static function onCompleteHandler(e:Event) {
dashboardObject = e.target.content;
// registerClassAlias("Dashboard", ); doesnt work
var dash:Class = getDefinitionByName("Dashboard") as Class;
var myDash = new dash();
trace(myDash.show);
}

Error

ReferenceError: Error #1065: Variable Dashboard is not defined.
at global/flash.utils::getDefinitionByName()
at System$/onCompleteHandler()

So, it seems that you cannot create an instance of the class if it is not executed as part of the SWF project. Which, if true, is what I want to do. I don’t want people trying to make instances of my classes only from loading a SWF file for what I am creating here.

thanks

+4
source share
5 answers

You need to do two things:

  • Give the Dashboard package ( package.to.Dashboard ). In classes that do not contain packages, different attributes (protected namespace) are provided in compiled form than those that have packages, which makes them inaccessible to external SWFs.
  • Make sure the loaded SWF is loaded in the same ApplicationDomain as the parent

Then you can use getDefinitionByName from the loaded SWF and new return Class .

+7
source
 var loader:Loader = new Loader(); var req:URLRequest = new URLRequest("foo.swf"); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fooLoadComplete); loader.load(req); function fooLoadComplete(e:Event):void { var ClassDefinition:Class = e.target.applicationDomain.getDefinition("Symbol1") as Class; var sym1:MovieClip = new ClassDefinition(); this.addChild(sym1); } 
+3
source

So, it seems that you cannot create an instance of the class if it is not executed as part of the SWF project.

Try the ModuleLoader class. See this article on how to create modules.

0
source

I recommend using CASALib . I created an entire application where I did not know the class names until run time. I simply made several interfaces to ensure that the most important functions are always available. The CASALib function, called LibraryManager , has a function for instantiating a class from an external SWF.

0
source

You might want to do this:

 var dash:Class = Loader(e.target).contentLoaderInfo .applicationDomain.getDefinition("Dashboard") as Class; 

getDefinitionByName() works for classes loaded by the current swf, and not from external swf. For external swf, you need to provide a link to the loader object that actually loaded the specific swf.

Also, if you do this in FlashBuilder, make sure that the “main application” in the downloadable SWF is Sprite (or maybe MovieClip, although I am not test), and not the application (since you end up in the default MXML file created for new SWF project). Otherwise, the above code will not be able to find the class definition you are looking for.

0
source

All Articles