Dynamic Vars Dictionary

Actionscript:
  1. var vars:Dictionary = new Dictionary();
  2.  
  3. var sp:Sprite = new Sprite();
  4.  
  5. // associate variables with a sprite (or any non-dynamic class)
  6. vars[sp] = {posX:100, posY:100, velX:1, velY:1};
  7.  
  8. // read
  9. 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.

This entry was posted in Dictionary, arrays, dynamic and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. jim booth
    Posted April 25, 2010 at 1:45 pm | Permalink

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*