2 Replies Latest reply on Mar 18, 2010 9:08 AM by UbuntuPenguin

    eventListener on variable change?

    SpaghettiCoder Level 3

      Wondering if it's possible and how would you go about doing it..

       

      Upon a public variable changing in a component, it executes code.

       

      [Bindable] is fine if I want to display the change as is.  I actually want to execute everytime the variable changes.

       

      I tried, addEventListener but that doesn't seem to work...is this even possible.

        • 1. Re: eventListener on variable change?
          UbuntuPenguin Level 4

          There are a few main ways you could do this. Let's assume the variable is named chess and is of the datatype Game

          1. ChangeWatcher ... moving on

          2. Or you could write your code like this.

           

             private var _chess:Game;

             public function set chess( value:Game ):void

             {

                  _chess = value;

                 dispatchEvent( new Event("gameChanger") );

             }

             [Bindable(Event="gameChanger")]

             public function get chess():Game

             {

                return _chess;

             }

           

             This method allows you to add an eventlistener that listens for the event "gameChanger".    Now if you want the chess game change event to bubble you may have to use a custom event.  An added advantage to this , is you can run any code you need to when the chess game changes in the set chess method.

          • 2. Re: eventListener on variable change?
            SpaghettiCoder Level 3

            Thanks, never even heard of the ChangeWatcher thing, gonna read up on that a bit more...

             

            Think I'm gonna use setters and getters, they seem to give me more control.