dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] Watch the value of a variable

  1. #1
    Captain
    Join Date
    Oct 2005
    Posts
    63

    resolved [RESOLVED] Watch the value of a variable

    Is it possible to add a "watchdog" to a variable so that it fires an event if that variable changes value?

    I know that I can have a loop testing every frame, but that seems unelegant.

    Code examples appreciated.

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You can put something like this in frame 1. You need to add an eventdispatcher.

    var myVar:String = "hello";
    var myTimer:Timer = new Timer(1);
    myTimer.start();
    myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
    function timerHandler(e:TimerEvent):void
    {
    if(myVar!="hello")
    {
    trace("changed");
    myTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
    }
    else
    {
    trace("not");
    }
    }
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I would set the variable as private, and set up getter and setter functions. Then in the setter, you can dispatch a custom event. Anything interested could listen for that event.

  4. #4
    Captain
    Join Date
    Oct 2005
    Posts
    63
    Of course. That's the way to do it! Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center