3 Replies Latest reply on Jul 14, 2006 7:01 AM by Newsgroup_User

    component initialization delay?

    Level 7
      Hi all,

      This must be a common question: I'm building a movieclip that I need to
      associate with a class. Within that movieclip, there is a button
      component (I'm using Flash 8 basic).

      In the constructor of my class, I'd like to immediately register a
      listener to the click event of the button but I am unable to do so as it
      seems the component is not initialized yet.

      How I solve this problem / how can I delay the listener registration?

      Here is a simplified version of my code that exhibit the problem:
      ==============================
      import mx.controls.Button;

      class UploadBox
      {
      private var browse_btn:Button;

      function UploadBox()
      {
      trace(browse_btn);
      trace(browse_btn.addEventListener);
      trace(browse_btn instanceof Button);

      browse_btn.addEventListener("click", this);
      }

      function click()
      {
      trace("click");
      }
      }
      ==============================

      output:
      ==============================
      _level0.instance1.browse_btn
      undefined
      false
      ==============================


      Any help would be much appreciated.

      Thanx,
      Tim.


        • 1. Re: component initialization delay?
          Level 7
          Replying to myself :).

          I implemented a really ugly hack (see below), but if anyone know a more
          elegant solution, please let me know.

          ==============================
          import mx.controls.Button;

          class UploadBox extends MovieClip
          {
          private var browse_btn:Button;

          function UploadBox()
          {
          onEnterFrame = setup;
          }

          function setup()
          {
          delete onEnterFrame

          trace(browse_btn);
          trace(browse_btn.addEventListener);
          trace(browse_btn instanceof Button);

          browse_btn.addEventListener("click", this);
          }

          function click()
          {
          trace("click");
          }
          }
          ==============================

          And that works, but manually using the onEnterFrame hack to delay
          constructor initialization by one frame's gotta be one of the ugliest
          hack I've done...

          Tim.



          Timothee Groleau wrote:
          > Hi all,
          >
          > This must be a common question: I'm building a movieclip that I need to
          > associate with a class. Within that movieclip, there is a button
          > component (I'm using Flash 8 basic).
          >
          > In the constructor of my class, I'd like to immediately register a
          > listener to the click event of the button but I am unable to do so as it
          > seems the component is not initialized yet.
          >
          > How I solve this problem / how can I delay the listener registration?
          >
          > Here is a simplified version of my code that exhibit the problem:
          > ==============================
          > import mx.controls.Button;
          >
          > class UploadBox
          > {
          > private var browse_btn:Button;
          >
          > function UploadBox()
          > {
          > trace(browse_btn);
          > trace(browse_btn.addEventListener);
          > trace(browse_btn instanceof Button);
          >
          > browse_btn.addEventListener("click", this);
          > }
          >
          > function click()
          > {
          > trace("click");
          > }
          > }
          > ==============================
          >
          > output:
          > ==============================
          > _level0.instance1.browse_btn
          > undefined
          > false
          > ==============================
          >
          >
          > Any help would be much appreciated.
          >
          > Thanx,
          > Tim.
          >
          >
          • 2. Re: component initialization delay?
            Peter Lorent Level 2
            I think you'll get it.

            import mx.controls.Button;

            class UploadBox extends MovieClip {

            private var browse_btn:Button;

            function UploadBox(){
            var waitInt=setInterval(function(ub:UploadBox):Void{
            if(ub.browse_btn.enabled!=undefined){
            trace("The Button is loaded.");
            ub.setup();
            clearInterval(waitInt);
            }
            },10,this);
            }

            function setup():Void{
            trace(browse_btn);
            trace(browse_btn.addEventListener);
            trace(browse_btn instanceof Button);
            this.browse_btn.addEventListener("click", this);
            }

            function click(){
            trace("click");
            }
            }
            • 3. Re: component initialization delay?
              Level 7
              > And that works, but manually using the onEnterFrame hack to delay
              > constructor initialization by one frame's gotta be one of the ugliest
              > hack I've done...


              lol - really?! I can think of plenty worse I've had to resort to over the
              years... :)



              --
              -------------------------------
              Remove '_spamkiller_' to mail
              -------------------------------