Created
July 26, 2009 16:54
-
-
Save brentp/155858 to your computer and use it in GitHub Desktop.
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
class Image extends Sprite { | |
public var path:String; | |
public var bitmap:Bitmap; | |
public var onLoaded:Bitmap->Void; | |
private var _loader:Loader; | |
/* take a path and a callback to be called when the image is loaded: | |
var i = new Image('/some/image.png', function(b:Bitmap){ | |
// do something with b or b.bitmapData... | |
trace('image is loaded'); | |
}); | |
*/ | |
public function new(path:String, onLoaded:Bitmap->Void=null){ | |
super(); | |
this.path = path; | |
this._loader = new Loader(); | |
this._loader.load(new URLRequest(path)); | |
this.onLoaded = onLoaded; | |
this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); | |
} | |
private function onComplete(e:Event) { | |
// access the data as Bitmap | |
this.bitmap = cast(this._loader.content, Bitmap); | |
this.addChild(this.bitmap); | |
this.onLoaded(this.bitmap); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment