Created
February 2, 2010 23:53
-
-
Save rjungemann/293186 to your computer and use it in GitHub Desktop.
ActionScript DynamicEvent, RemoteClass, and RemoteClassSamples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a simple pure-AS3 DynamicEvent class | |
package com.thefifthcircuit.utils { | |
import flash.events.Event; | |
public dynamic class DynamicEvent extends Event { | |
public function DynamicEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false) { | |
super(type, bubbles, cancelable) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load a class from a remote swf | |
package com.thefifthcircuit.utils { | |
import flash.display.Loader; | |
import flash.events.EventDispatcher; | |
import flash.events.Event; | |
import flash.net.URLRequest; | |
import com.trustnode.bikegame.DynamicEvent; | |
public class RemoteClass extends EventDispatcher { | |
private var loader:Loader; | |
private var url:String, klassName:String; | |
public var loaded:Boolean = false; | |
public var remoteClass:*; | |
public function RemoteClass(url:String, klassName:String):void { | |
loader = new Loader(); | |
this.url = url, this.klassName = klassName; | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, klassLoaded); | |
loader.load(new URLRequest(url)); | |
} | |
public function complete(callback:Function):void { | |
if(!loaded) { | |
addEventListener("loaded", callback); | |
} else { | |
var event:DynamicEvent = new DynamicEvent("loaded"); | |
event.remoteClass = klass; | |
callback(event); | |
} | |
} | |
private function klassLoaded(e:Event):void { | |
loaded = true; | |
klass = loader.contentLoaderInfo.applicationDomain.getDefinition(klassName); | |
var event:DynamicEvent = new DynamicEvent("loaded"); | |
event.remoteClass = klass; | |
dispatchEvent(event); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url:String = "DisplaySampleTest.swf"; | |
var klassName:String = "com.thefifthcircuit.displaySampleTest.DisplaySample"; | |
var util:RemoteClass = new RemoteClass(url, klassName); | |
util.complete(function(e:*):void { | |
var displaySample:* = new e.klass(); | |
trace(displaySample.foo); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment