Domanda

How do you duplicate the content of the 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>;
    }

It returns null for the slotV2. Or in this case would a multidimensional Vector would be a better way? I'm not sure what other info I would add into the MC in the future though.

È stato utile?

Soluzione

You cannot deep-clone a vector of DisplayObjects of any kind, unless the objects' classes implement IExternalizable. This is because deeply cloning a DisplayObject requires cloning all its references including the entire children list and stage, and you cannot make another stage. Thus, you'd better create your "deep clone" by creating more instances of SlotClass in the cycle and stuffing them into corresponding vectors.

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);
    }

Don't forget to initialize the vectors prior to running the cycle.

As a side point, yes it might be better to use a multidimensional vector, considering why do you need these several vectors.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top