I know this has been covered a lot, but I just can’t seem to get it to work.
How do I listen from the main class to a sub class for a variable change?
package {
import flash.events.*
public class DataBase extends MovieClip {
private var _OPPONENTTYPE:int = 0;
public function DataBase() {
OPPONENTTYPE(1);
}
public function get OPPONENTTYPE():int {
return _OPPONENTTYPE;
}
public function set OPPONENTTYPE(num:int) {
_OPPONENTTYPE = num;
dispatchEvent(new Event(DataBase._OPPONENTTYPE));
}
This is what I have so far for the sub class but I can’t seem to figure out how to listen to this variable change from Main() or even if I am dispatching the event properly.
Assuming that Main has a reference to the Database class you could do something like this:
First, in Database declare a public const to hold the name of your event. This is better than using a string in your event code because it allows editors to error check and auto-complete.
Now modify your setter to use this const:
In Main you need to listen for the event (assuming that you have already instantiated and made a reference to Database):
And finally, in the handler you check the new value of OPPONENTTYPE by looking at the target of the event object, which will be your database class, or by using the reference you used when setting up the listener: