Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 Instantiate Class From External SWF

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

like image 732
Chris Avatar asked Dec 30 '22 12:12

Chris


2 Answers

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.

like image 141
Richard Szalay Avatar answered Jan 09 '23 21:01

Richard Szalay


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);
}
like image 27
ash Avatar answered Jan 09 '23 23:01

ash