I have this small class called City that simply holds some information about a city, here it is:
class com.weatherwidget.City {
var zipCode:String;
var forecastText:Array = new Array(5);
}
When I have an array of cities and I change one of the forecastText of one city it will change that forecastText for all of the cities.
For example:
import com.weatherwidget.City;
var arr:Array = new Array();
arr.push(new City());
arr.push(new City());
arr[0].forecastText[0] = "Cloudy";
trace(arr[0].forecastText[0]);
trace(arr[1].forecastText[0]);
Will have the following output:
Cloudy
Cloudy
Even though I only changed arr[0].forecastText[0]. I think I must be misunderstanding something about arrays in objects for actionscript 2.
well the reason why … hmmm … little complicated to explain …
alright … ActionScript is prototype-oriented, as is ECMA-script … classes are only a syntactic sugar introduced by actionscript 2 (this has changed yet again in as3, but that’s a different subject) …
so if this is the original code:
then this is, what actually happens:
the prototype object of
City, that serves as prototype for instances ofCity, has a property calledforecastText, which is anArrayof length 5 … so when looking upforecastTexton an instance ofCity, it cannot be found directly and will be looked up in the prototype chain … it will be found in the instance’s prototype … thus, all instances share the sameArray…the difference is, that the second example gets translated to:
as you might have noticed, declared members are only a compiletime thing … if nothing is assigned to them, they simply will not exist at runtime …
well, this explenation requires, that you either know JavaScript or ActionScript 1 a little, but i hope it helps …
greetz
back2dos