views:

1265

answers:

5

Hi,

I was chatting with my buddy about this, he is convinced you can do this and says he has done it, but I cannot get this to work.

I am wondering if it is even possible at all. I tried typing a var as a Class that is within the externally downloaded SWF and then making an instance but no can do.

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 you cannot make an instance of a class unless it is complied within the project SWF. Which if true is what I want it to do. I do not want people trying to make instances of my classes just from downloading the SWF file for what I am building here.

thanks

A: 

So it seems you cannot make an instance of a class unless it is complied within the project SWF.

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

dirkgently
I just wanted to confirm with someone or many about if this was possible or not, because they could easily make an instances of a static class and change vars that you would rather have them not change if it was an API your building, and you need that certain var to be public for use in your API. This article seems to be for flex which I do not deal with currently. Thank You
Chris
+6  A: 

You need to do two things:

  1. Give Dashboard a package (package.to.Dashboard). Package-less classes are given different attributes (a protected namespace) in compiled form than those with packages, making them inaccessible to external SWFs.
  2. Ensure that your loaded SWF is loaded into the same ApplicationDomain as the parent

You should then be able to use getDefinitionByName from the loaded SWF and new the return Class.

Richard Szalay
A: 

I recommend using CASALib for this. I've created an entire app where I didn't know the class names until runtime. I just made some interfaces to ensure that the most important functions were always available. The CASALib util called LibraryManager has a function for instantiating a class from an external SWF.

Sandro
A: 

You might want to do this:
var dash:Class =Loader(e.target).contentLoaderInfo.applicationDomain.getDefinition("Dashboard") as Class;
getDefinitionByName() works for the classes loaded by the current swf not the external swfs. For external swf you need to give the reference of the loader object which actually loaded the particular swf.

bhups
A: 
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);
}
ash