views:

20

answers:

1

i'm currently using a dictionary to associate a boolean to my (non-dynamic) sprites, but i would like to know if there is a smarter way of doing this? i could just use MovieClips to assign my properties instead of Sprites since MovieClips are dynamic, but i will not be using any of the MovieClip properties or functions so it comes down to a best practice issue.

basically i want to create a state boolean property on my sprites - they are either on or off so my boolean variable is called isOn.

var mySprite:Sprite = new Sprite();
var isOn:Boolean = false;

var dict:Dictionary = new Dictionar();
dict[mySprite] = isOn;

then i will poll my sprite to check its "isOn" property. if it's on, i will turn it off - or set it to false.

if (dict[mySprite] == true)
   {
   dict[mySprite] = false;
   }

this is the first time i'm actually using dictionaries, so please correct me if i'm using it wrong. and, of course, my original question stands: is this the best way of adding a boolean property to a non-dynamic object?

+1  A: 

Can't you just write your own Sprite that has an isOn property? That seems like a much simpler way to achieve what you want, without using a MovieClip.

isOn could be a public var or a pair of getter/setter if you want to perform some logic when reading/writting it.

public class MySprite extends Sprite {

    private var _isOn:Boolean;

    public function get isOn():Boolean {
        return _isOn;
    }

    public function set isOn(v:Boolean):void {
        _isOn = v;
    }

}

And then:

var mySprite:MySprite = new MySprite();
mySprite.isOn = false;

// at some later point...
if (mySprite.isOn)
{
   mySprite.isOn = false;
}
Juan Pablo Califano
oh, yes that would be the way to go usually, but i forgot to mention that i'm creating all of my sprites in Flash Authoring and then exporting them for ActionScript.
TheDarkInI1978
In that case, in the Flash IDE, you could have your symbol extend MySprite instead of Sprite.
Juan Pablo Califano