复制/克隆Vector。<movieclip> </movieclip>

时间:2014-05-07 10:45:05

标签: actionscript-3 vector duplicates

如何复制Vector的内容。?

private var slotMC:MovieClip;
    private var slotV1:Vector.<MovieClip> = new Vector.<MovieClip>();
    private var slotV2:Vector.<MovieClip>;
    private var slotV3:Vector.<MovieClip>;

    public function InputSlot():void {
        registerClassAlias("MovieClip", MovieClip);
        for (var i:int = 1; i < typeAmount + 1; i++) {                      
            SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot0' + i) as Class;
            slotMC = new SlotClass();
            slotMC.name = "slot" + i;

            //push to vector before randomly add to stage
            slotV1.push(slotMC);
        }

        slotV2 = clone(slotV1);
        trace('slotv2', slotV2);
    }

    private function clone(source:Vector.<MovieClip>):Vector.<MovieClip> {  
        var myBA:ByteArray = new ByteArray();
        myBA.writeObject(source);
        myBA.position = 0;

        return myBA.readObject() as Vector.<MovieClip>;
    }

它为slotV2返回null。 或者在这种情况下,多维向量会更好吗? 我不确定将来我会在MC中添加哪些其他信息。

1 个答案:

答案 0 :(得分:0)

你无法深度克隆任何类型DisplayObject的向量,除非对象&#39;类实现IExternalizable。这是因为深度克隆DisplayObject需要克隆其所有引用,包括整个子列表和阶段,并且您不能再创建另一个阶段。因此,您可以更好地创建您的深层克隆&#34;通过在循环中创建更多SlotClass的实例并将它们填充到相应的向量中。

for (var i:int = 1; i < typeAmount + 1; i++) {                      
        SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot0' + i) as Class;
        slotMC = new SlotClass();
        // slotMC.name = "slot" + i; 
        // drop name setting, you'd better use position in vector to refer to the clip

        //push to vector before randomly add to stage
        slotV1.push(slotMC);
        slotMC = new SlotClass();
        slotV2.push(slotMC);
        slotMC = new SlotClass();
        slotV3.push(slotMC);
    }

不要忘记在运行循环之前初始化载体。

作为一个侧面点,是的可能更好地使用多维向量,考虑为什么需要这几个向量。