Actionscript:
-
var vars:Dictionary = new Dictionary();
-
-
var sp:Sprite = new Sprite();
-
-
// associate variables with a sprite (or any non-dynamic class)
-
vars[sp] = {posX:100, posY:100, velX:1, velY:1};
-
-
// read
-
trace(vars[sp].posX);
I've heard people mention that they wish the sprite class were dynamic... meaning they wish they could add methods and properties to a Sprite instance at runtime. There's no way I know of to do this, however... the dictionary class can be used to associate variables with any non-dynamic class instance... as it does in this the above example.
The dictionary class is similar to an associative array except that instead of using strings for keys, dictionaries use object instances.
One Comment
is there any way to retrieve the names of the variables.. ie the posx and posy? I am putting the data in dynamically and would like to check to see if the variable has been populated first.. also, I would like to be able to add to the list such as.. if vars[sp]={posX;100};
later add the vars[sp]={posX;100,posY;90};