Return to Snippet

Revision: 13535
at April 27, 2009 09:35 by dx0ne


Initial Code
import flash.display.DisplayObject;
import flash.display.LoaderInfo;
import flash.system.LoaderContext;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
...
// this is where we create the loader:
var loader:Loader = new Loader();
// this forces the loader to look for a crossdomain.xml file
var lctx:LoaderContext = new LoaderContext(true);
// listen for the init event on the loader
loader.contentLoaderInfo.addEventListener(Event.INIT, doImgInit);
// load the image, here the path is not valid.. i made it up
loader.load(new URLRequest("http://www.cssq.com/dunno.png", lctx);
....
// inside of the doImgInit function
protected function doImgInit(evt:Event):void
{
	// get a reference to the LoaderInfo object in which the image is loaded
	var lInfo:LoaderInfo = evt.target as LoaderInfo;
	// this variable is used as reference to the image in the end.
	var dO:DisplayObject;
	// try to access the "content" property of the loader, if it works, there is a crossdomain.xml file.
	try{
		dO = lInfo.loader.content;
	}
	// if there wasn't one, we need to put the loader inside another object in order to manipulate it
	catch(err:SecurityError)
	{
		// create a new Sprite to contain the loaded image
		var sprt:Sprite = new Sprite();
		// add the loader to the sprite
		sprt.addChild(lInfo.loader);
		// get a reference to this sprite in the dO variable
		var dO:DisplayObject = sprt as DisplayObject;
	}
	// from here on you can do anything to the dO variable, rotate it, draw it unto a bitmapData, move it around..
	// but first don't forget to add to to some container that is on the stage so you can see it!
}

Initial URL
http://blog.martinlegris.com/?p=82#more-82

Initial Description


Initial Title
Getting around the crossdomain.xml file when loading images in as3

Initial Tags


Initial Language
ActionScript 3