all groups > flash actionscript > september 2004 >
You're in the

flash actionscript

group:

array assignment and linkage


array assignment and linkage x-crow
9/7/2004 9:32:45 PM
flash actionscript:
Hello,
I'm having a problem with an array Linkage issue, the best way to explain it
is to show you the code and tell you after you've copy and pasted and seen the
results:
-------------------Code----------------------
var my_car:Array = new Array({make:"none"});
trace("...> Assigning Dodge to My_Car[0]");
my_car[0].make = "Dodge";
trace("My Car: " + my_car[0].make);
trace("Temp: " + temp_ar[0].make);
trace("...> Assigning my_car to temp_ar");
var temp_ar:Array = my_car;
trace("My Car: " + my_car[0].make);
trace("Temp: " + temp_ar[0].make);
trace("...> Assigning Ford (only) to temp_ar[0]");
temp_ar[0].make = "Ford"
trace("My Car: " + my_car[0].make);
trace("Temp: " + temp_ar[0].make);
-------------end of code------------------

the important part is the last two trace statements, which end up stating
that:
-------------------
My Car: Ford
Temp: Ford
-------------------
Now if you follow the code, I've only put Temp_ar[0].make = "Ford" but because
for some reason, unknown to me, the two arrays are linked and therefore
my_ca[0] also changes without any code...

Any Help will be Golden...

Thanks,

Andrew
Re: array assignment and linkage blenz
9/7/2004 9:57:28 PM
Arrays in flash are created by reference, it means , when you create an
variable that is an Array it created a "reference" to an array,.. in so doing
if you did this :
var temp_ar:Array = my_car;

temp_ar is referencing the same array that my_car is referencing.. so if
either of them changed the contents of the array, both of them reference it.

a better construction to achieve what you want is

var temp_ar:Array = new Array({make:"none"});
temp_ar[0].make=my_car[0].make;

in that way you are assured that temp_ar is referencing a different array that
my_car is referencing


Re: array assignment and linkage mandingo
9/7/2004 11:43:04 PM
if your problem stems from the fact you have a large array that you need to
make a copy of before editing only one of the two new arrays, then make a copy
like this:

var temp_ar:Array = new Array({make:"none"});
for(var x=0;x<my_car.length;x++){
temp_ar[x].make = my_car[x].make;
}

I hope that helps,
cheers
Re: array assignment and linkage x-crow
9/8/2004 7:32:19 PM
that did help me, but one problem, if I have more then just the make of the car
in the array, I can't just say
-------code----------
var temp_ar:Array = new Array({make:"None",topSpeed:0,colour:"None"})
for(x in my_car){
temp_ar[x] = my_car[x]
}
-------end----------
because it still links them, so I have to loop and completely re-assign all
the values of that array? that really blows, MM should really have a command to
just copy the array...

thanks again,

Andrew
Re: array assignment and linkage mandingo
9/9/2004 1:27:20 AM
ok... so to make a new copy from an existing... where you don't necessarily
know the elements of the arrays,

my_car:Array =
[{make:"Ford",topSpeed:160,colour:"Black"},{make:"Dodge",topSpeed:180,colour:"Wh
ite"}]

var temp_ar:Array = new Array({make:"None",topSpeed:0,colour:"None"})
for(var z=0;z<my_car.length;z++){
temp_ar[z]:Array = new Array();
for(x in my_car[z]){
trace(x + " : " + my_car[z][x])
temp_ar[z][x] = my_car[z][x];
}
}
temp_ar[1].make = "Buick";
trace(my_car[1].make);
trace(temp_ar[1].make);

hth
cheers,
Re: array assignment and linkage x-crow
9/9/2004 4:21:09 PM
Thanks, that's just what I was about to do... I just forgot about that function
when writing my reply ;)
I think I might just extand that Array Class and add a my_array.copy()
function to return an copied array... if it works, I'll try to post the AS here
;)
AddThis Social Bookmark Button