1 Reply Latest reply on Mar 14, 2011 6:57 PM by Ned Murphy

    AS3 delay help

    Anthizumal

      Hi, I am using AS3 and have made a function called anotherPic which occurs when the user clicks the mouse.  However, I would like to

      delay the function so the user clicks the mouse, then 3 seconds later the function runs.  Here is the code I have so far:

       

      btnAnother.addEventListener(MouseEvent.CLICK, anotherPic);

       

      function anotherPic(mevt:MouseEvent):void {

       

      btnAnother.visible = false;

      removeChild(thisBmp);

       

      picLoader = new Loader();

       

      var stImage:String = xmlData.pix[Math.floor(numImages*Math.random())].toString();

       

      picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotPic);

      picLoader.load( new URLRequest(stImage) );

      }

       

      Thanks so much

        • 1. Re: AS3 delay help
          Ned Murphy Adobe Community Professional & MVP

          If the anotherPic function loads the image, then leave it as is except remove the mevt:MouseEvent argument.  Have the button click call another function that has a setTimeout function call in it... the setTimeout can call your anotherPic function after it times out.

           

          btnAnother.addEventListener(MouseEvent.CLICK, wait3Secs);

           

          function wait3Secs(mevt:MouseEvent):void {

               var sto = setTimeout(anotherPic, 3000);

          }

           

          function anotherPic():void {

           

              btnAnother.visible = false;

              removeChild(thisBmp);

           

              picLoader = new Loader();

           

              var stImage:String = xmlData.pix[Math.floor(numImages*Math.random())].toString();

           

              picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotPic);

              picLoader.load( new URLRequest(stImage) );

          }