i’ve never tried to do this before, so my head a swimming a bit. i’d like to have a public boolean called enabled in myClass custom class. if it’s called to be changed, how do i trigger a function from the change?
should i add an Event.CHANGE event listener to my variable? can i do that? or is there a more standard way?
We usually use properties for that.
Properties are just like public variables for the outside — you can set
instance.enabled = true;and so forth.. But you define properties as getters and/or setters functions for the class.They are the perfect place for custom logic to be executed on value changes.
For example:
Note that they can’t have the same name as your private variable and you don’t need both of them defined. Actually, you don’t even need a variable for a setter/getter. You could use them just like any function — they just supply you with a different syntax.
For example:
They are great to expose a simple API, keeping you from rewriting outside code if the class’ internals have to change.
AS3 Reference on getters and setters.