PDA

View Full Version : push copy of object onto array



ryoaska
May 6th, 2004, 05:22 PM
hey, I was wondering if there's any way to push a copy of an object onto an array without having to create a seperate variable for each object.

For example- in c, i'm used to doing something like having a 'temp' object- and then in a loop i would set the value of the objects members and push it on to the array (vector). If i try to do something like that in flash though, it stores a reference to that object in the array, so every time it changes every new element in the array changes to have the latest values of the temp object.

Without using as2.0 classes (i.e. just mx style) is there any way I can get the effect i'm trying to get here- just have one instance of an object that i can redefine in a loop and push a copy of it into the array so that i wouldn't have to declare a separate variable for each one which would defeat the purpose of the array.

any help would be appreciated- thanks

Clown Staples
May 6th, 2004, 05:48 PM
reallocate the object each time through the loop using var.

var theArray = []; // or new Array();
for (var i=0;i<20;i++){
var obj = {}; // or new Object();
obj.x = 2;
obj.y = 7;
theArray.push(obj);
}

ryoaska
May 7th, 2004, 06:07 PM
ah- smooth move, thanks a lot man I wouldn't have thought of that