Friday, April 16, 2010

Reparent AVM1 content in AVM2 (AS3) content.

For whatever reason you need to reparent loaded AVM1 content to another part of the display list and you don't want to keep the loader as the parent. One would have to save the content first from the LoaderInfo instance then use the LoaderInfo loader member to unload the loaded content.

You will then be allowed to reparent the saved content elsewhere. But be warned if you try to reparent it again you will get the:

"ArgumentError: Error #2180: It is illegal to move AVM1 content (AS1 or AS2) to a different part of the displayList when it has been loaded into AVM2 (AS3) content."

error as you would with the original. Remove the child first before you reparent.

Example from the Event.COMPLETE dispatch:
var li:LoaderInfo = e.target as LoaderInfo;
var avm1:AVM1Movie = li.content as AVM1Movie;
var l:Loader = li.loader;
l.unload();

// No error
layer1.addChild(avm1);

// No error
layer1.removeChild(avm1);
layer2.addChild(avm1);

// Error #2180
layer3.addChild(avm1);

2 comments:

United Church of Christ said...

not quite sure I understand the code here, here is what I am currently using:

var myLoader:Loader = new Loader();
addChild(myLoader);
var url:URLRequest=new URLRequest("BannerRotator.swf");
myLoader.load(url);
myLoader.x=13;
myLoader.y=13;

which gives the 2180 error.

where do i insert BannerRotator.swf into your code to make it work?

Rob said...

The code in the post is suppose to be in the method called from the Event.COMPLETE dispatch. Although you can add the loader to the display list; my example is for the content itself. If you cut and paste the code below in the Flash IDE it should work if the "BannerRotator.swf" is in the same root folder as the outputted SWF.

//---BEGIN---

var url:URLRequest=new URLRequest("BannerRotator.swf");

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onReqComplete);
l.load(url);

function onReqComplete(e:Event):void
{
var li:LoaderInfo = e.target as LoaderInfo;
var disObj:DisplayObject = li.content as DisplayObject;
var l:Loader = li.loader;
l.unload();

addChild(disObj);
}

//---END---