http://asgrocery.blogspot.com/2010/02/actionscript-3-duplicate-stage-instace.html#more
很有趣的構思
[actionscript3]
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
/**
* …
* @author Focus1921
*
* 複製無綁定類別movie clip場景實體(stage instance)實作demo(fla文件類別)
*/
public class DocTest extends MovieClip {
//stage instance
public var test_box_mc:MovieClip; //場景上僅有的movie clip, 實體名稱test_box_mc
private var ba:ByteArray; //儲存自己這個swf的byte array
private var ldr:Loader; //建一個loader來load ba
private var tmpX:Number;
private var tmpY:Number;
/**
* 建構式
*/
public function DocTest() {
if (stage) {
init();
}else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//確保主場景load只load自己一次,避免溢位
if (parent is Stage) {
trace("parent is Stage");
//建構url loader(binary)去load自己的byte array
var _urlLoader:URLLoader = new URLLoader();
_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
//trace(root.loaderInfo.url);
_urlLoader.load(new URLRequest(root.loaderInfo.url));
_urlLoader.addEventListener(Event.COMPLETE, onURLLdrComplete);
}
//建構loader, 註冊complete完成偵聽
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLdrComplete);
//stage註冊click偵聽
stage.addEventListener(MouseEvent.CLICK, onStageClick);
}
//—Event Handler—————————————————————————–
private function onURLLdrComplete(e:Event):void {
e.currentTarget.removeEventListener(Event.COMPLETE, onURLLdrComplete);
//把load到的byte array存起來
ba = e.currentTarget.data;
}
private function onLdrComplete(e:Event):void {
//用實體名稱去抓到要複製的movie clip,每次load完都會傳不同的引用
var _mc:MovieClip = <div style="display: inline; height: auto; position: absolute; visibility: hidden; width: auto; ">MovieClip</div>MovieClip(ldr.content).getChildByName("test_box_mc") as MovieClip;
//屬性設定不同的值,證明的確不是同一個引用
_mc.x = tmpX;
_mc.y = tmpY;
_mc.alpha = Math.random();
//加入顯示清單
addChild(_mc);
}
private function onStageClick(e:MouseEvent):void {
//把點擊座標記起來,當作複製後的mc的座標
tmpX = e.stageX;
tmpY = e.stageY;
//loader去load(loadBytes)存起來的byte array(ba)
ldr.loadBytes(ba);
}
}
}
[/actionscript3]