1 Reply Latest reply on Nov 21, 2006 4:19 AM by odbarnes

    calling setTimeout within custom class and cannot access the class' variables

    odbarnes
      Hi All,

      I'm creating an user interface with Flash 8, I'm new to Flash, my background is Java therefore I decided to use ActionScript to create a custom component that is reference within an external file.

      My problem I believe is scope related. I have a status bar (label component), once I update the status bar I want to call the
      "setTimeout" function to wait a couple of seconds before clearing my status bar. The "setTimeout" function is executing, but for some reason my status bar is not being cleared (accessed).

      Below is a snippet of my code. Any help appreciated. Thanks.


      /*
      External File
      */
      import mx.controls.Button;
      import mx.controls.Label;

      class MyApp
      {
      //private properties
      private var mc_container:MovieClip;
      private var btn:Button;
      private var lbl_status:Label;
      private var str:String = "do see me";

      //constructor
      public function MyApp(target:MovieClip)
      {
      trace("-- MyApp --");
      mc_container = target.createEmptyMovieClip("mc_container", 100);
      }
      //public methods
      public function init(width:Number, height: Number, xPos:Number, yPos:Number):Void
      {
      var my_app:MyApp = this;

      btn = mc_container.createClassObject(Button, "btn", 1, {label:"click me"});
      btn.setSize(100, 25);
      btn.move(10, 10);
      btn.clickHandler = function():Void
      {
      trace("btn clicked");
      setTimeout(my_app.clearStatus, 2400);
      //correct if I call directly
      //my_app.clearStatus();
      trace(str);
      }

      lbl_status = mc_container.createClassObject(Label, "lbl_status", 2, {text:"status bar message"});
      lbl_status.setSize(200, 25);
      lbl_status.move(0, 50);
      }
      public function clearStatus():Void
      {
      trace("calling clearStatus");
      lbl_status.text = "";
      }
      }


      /*
      Called for Flash IDE
      */
      var app:MyApp = new MyApp(this);
      app.init(200, 200, 0, 0);
        • 1. Re: calling setTimeout within custom class and cannot access the class' variables
          odbarnes Level 1
          Found an solution; send the component I want to reference as an argument in setTimeout, posted results below:

          import mx.controls.Button;
          import mx.controls.Label;

          class MyApp
          {
          //private properties
          private var mc_container:MovieClip;
          private var btn:Button;
          private var lbl_status:Label;

          //constructor
          public function MyApp(target:MovieClip)
          {
          trace("-- MyApp --");
          mc_container = target.createEmptyMovieClip("mc_container", 100);
          }
          //public methods
          public function init(width:Number, height: Number, xPos:Number, yPos:Number):Void
          {
          var my_app:MyApp = this;

          btn = mc_container.createClassObject(Button, "btn", 1, {label:"click me"});
          btn.setSize(100, 25);
          btn.move(10, 10);
          btn.clickHandler = function():Void
          {
          trace("btn clicked");
          setTimeout(my_app.clearStatus, 6000, my_app.lbl_status);
          //correct if I call directly
          //my_app.clearStatus();
          trace(str);
          }

          lbl_status = mc_container.createClassObject(Label, "lbl_status", 2, {text:"status bar message"});
          lbl_status.setSize(200, 25);
          lbl_status.move(0, 50);
          }
          public function clearStatus(lbl:Label):Void
          {
          trace("calling clearStatus");
          lbl.text = "";
          }
          }