I am trying to load an external swf (AS3) in an AS3 application, using basically a duplicateMovieClip() function from the “Tip of The Day” (the loaded swf should appear in several places in the Container - each with its own timeline - that is, I want to be able to play() ant stop() them independently); ofcourse, I’m trying to avoid loading the same file multiple times.
The problem is that it doesn’t work. This is the code, and I would appreciate any suggestions:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="main()" width="500" height="500">
<mx:UIComponent id="spriteHolder"/>
<mx:Script>
<![CDATA[
public var swfCovers:Array = new Array;
public function main():void{
var swfLoader:Loader = new Loader;
swfLoader.load(new URLRequest("assets/Slave.swf"));
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addSwf);
function addSwf():void{
var i:Number;
for(i=0; i<4; i++){
swfCovers* = MovieClip(duplicateDisplayObject(MovieClip(swfLoader.content)));
swfCovers*.x = i * 100;
swfCovers*.y = i * 100;
spriteHolder.addChild(swfCovers*);
}
}
function duplicateDisplayObject(target:DisplayObject):DisplayObject {
// create duplicate
var targetClass:Class = Object(target).constructor;
var duplicate:DisplayObject = new targetClass();
// duplicate properties
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
if (target.scale9Grid) {
var rect:Rectangle = target.scale9Grid;
// Flash 9 bug where returned scale9Grid is 20x larger than assigned
rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
duplicate.scale9Grid = rect;
}
return duplicate;
}// endfunc duplicateDisplayObject()
}
]]>
</mx:Script>
</mx:Application>
Behavior: with a swf created in Flash CS3, FlashPlayer simply crashes; with a swf created in Flex 2.0, nothing appears on screen, no error messages given.
This is a simple swf in Flex:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Panel title="My Application" width="186" height="300">
<mx:Label text="Welcome to Flex!" mouseDownEffect="WipeRight"/>
</mx:Panel>
</mx:Application>