July 08, 2009

Load swf content into the Sprite in AS3

Hi,
In some of the post has mention as cannot possible to load the swf content inside the Sprite using the Loader class in AS3. But i try to load the swf content into the sprite using the Loader class. In this below example to load the loader directly instead of adding the child as loader.content. If you are try to load the child as loader.content means it throws an error as AVM1 object voliation for load the AVM2 oject into AVM1 Object. So you can add the child of sprite as loader it can possible to load the swf content and you can work over that.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onInit()">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import flash.events.KeyboardEvent;
import mx.controls.SWFLoader;

private var ui:UIComponent;
private var _spr:Sprite;
private var loader:Loader;

private function onInit():void
{
ui = new UIComponent();
_spr = new Sprite();
ui.addChild(_spr);
addChild(ui);

loader = new Loader();
loader.load(new URLRequest("test.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
}

private function onLoadComplete(e:Event):void
{
_spr.addChild(loader);
}

]]>
</mx:Script>
</mx:Application>

No comments:

Post a Comment