18 Replies Latest reply on Aug 4, 2006 7:05 AM by Goo101

    Dynamic Text

    Goo101
      Hi there,
      I'm trying to use a dynamic text field to display an error message, using the following code . . .

      if (VoucherNo.length < 14) {
      //changes the dynamic text field to display an "Invalid Voucher Number" message
      this.dtxtVoucherNo = "Invalid Voucher Number!";
      //returns to the main screen
      if((this._parent.Main != undefined) && (this._parent.Main != null))
      {
      var screen = null;
      var target = this;
      while((screen == null) && (target != undefined) && (target != null))
      {
      if(target instanceof mx.screens.Screen)
      {
      screen = target;
      }
      else
      {
      target = target._parent;
      }
      }
      if(screen instanceof mx.screens.Slide)
      {
      screen.gotoSlide(this._parent.Main);
      }
      }

      // then clears down the text in the itxtVoucherNo field
      this.itxtVoucherNo.text = "";
      trace("Voucher Number is invalid! (1)") }

      All the above code is set within the on (release) fucntion of a button object. However, the compiled test only displays this error message for a split second before returning to the main screen. I need to put some sort of time delay in here to allow the application to display this error message before going back to the Main screen. I have tried using a MovieClip object to provide the time delay but it didn't work!

      Does anyone know how I can do this? Please!

      Thanks.
        • 1. Dynamic Text
          animee
          I don't know if i really understand your problem.

          Why don't you create a clip instance holding that textfield and then add the number of frames you want the text to be displayed?


          • 2. Re: Dynamic Text
            Goo101 Level 1
            Probably because I don't understand frames, just AS.

            Anyway, what I want to do is to display the changed dynamic text and then return to the main screen (using the gotoSlide logic). But I need some sort of delay in here before returning to the main screen so that the customer gets a chance to catch the error message.

            I tried the setInterval logic but it doesn't seem to work in this instance. I think it only works if it's assigned to a function.

            Help?
            • 3. Re: Dynamic Text
              zensoldier Level 1
              Use setTimeout()

              or have the user click continue after they are done reading the error message
              • 4. Re: Dynamic Text
                Goo101 Level 1
                There is no such function as setTimeout() (or even setTimeOut()), where did you get this from?

                It would be lovely if there were but I think the only way of doing this is by using frames and putting different sections of code on different frames?

                Does anyone else have any suggestions?
                • 5. Re: Dynamic Text
                  Wolf_van_Ween Level 1
                  of course setInterval(). What do you mean by "only works if it's assigned to a function"?
                  The opposite is true. You need to assign a call-back function to the interval. So in your case, the code that closes the error message window needs to go into a function, and the setInterval goes into the place of this code in the old script.
                  Like this
                  Old:
                  create error message
                  close error message
                  continue

                  New:
                  create error message
                  errorInterval = setInterval("closeError",3000);
                  continue

                  function closeError():Void {
                  close error message
                  delete errorInterval;
                  }
                  • 6. Re: Dynamic Text
                    Goo101 Level 1
                    No, again I cannot get the setInterval() command to work here . . .

                    My code now looks like . . .

                    // Checks the validity of VoucherNo based to the last good known Voucher Number
                    // and upper Voucher range (based on tomorrow's date)
                    if (VoucherNo.length < 14) {
                    //changes the dynamic text field to display an "Invalid Voucher Number" message
                    this.dtxtVoucherNo.text = "invalid voucher number!";
                    //pauses the app. for 10 seconds
                    invalidInterval = setInterval("changedtxt", 10000);
                    continue;
                    function changedtxt():Void {
                    this.dtxtVoucherNo.text = "enter your voucher number";
                    delete invalidInterval;
                    }
                    //then returns the app. to the Main screen
                    // GoTo Screen behavior
                    if((this._parent.Main != undefined) && (this._parent.Main != null))
                    {
                    var screen = null;
                    var target = this;
                    while((screen == null) && (target != undefined) && (target != null))
                    {
                    if(target instanceof mx.screens.Screen)
                    {
                    screen = target;
                    }
                    else
                    {
                    target = target._parent;
                    }
                    }
                    if(screen instanceof mx.screens.Slide)
                    {
                    screen.gotoSlide(this._parent.Main);
                    }
                    }
                    // End GoTo Screen behavior
                    }

                    If I comment out the gotoSlide logic the screen does indeed display the "invalid voucher number!" message in the dynamic text field but as soon as I uncomment the gotoSlide logic, the app. returns to the main screen so fast that you don't even get a chance to read the error message in the dynamic text field. I still some sort of time delay in here which works...

                    I've tried using frames by placing the gotoSlide logic in a different frame, I've tried using the setInterval command, the setTimeOut() command does not exist. Surely there is a way to do this using AS.

                    Can anyone out there help!? Please!
                    • 7. Dynamic Text
                      zensoldier Level 1
                      Of course there is a setTimeout()

                      adobe reference
                      http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context= LiveDocs_Parts&file=00001717.html

                      Read the text at the bottom of the function list about setTimeout()

                      setTimeout has been a part of javascript forever and actionscript has always supported it. Its just not in macromedia (adobe) documentation because their docs suck.

                      javascript reference to setTimeout()

                      http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods /settimeout.asp

                      You said it would be gret if there was such a thing, well there you go, have a ball. I've been using it for a long time and it works just great.

                      • 8. Re: Dynamic Text
                        Goo101 Level 1
                        Excellent, thanks so much zensoldier!

                        But . . . I still can't get this logic to work as it should . . . can you please take a look at the following code and let me know where on earth I'm going wrong . . . this is driving me mad!

                        // Checks the validity of VoucherNo based to the last good known Voucher Number and upper Voucher range (based on tomorrow's date)
                        if (VoucherNo.length < 14) {
                        //changes the dynamic text field to display an "Invalid Voucher Number" message
                        this.dtxtVoucherNo.text = "invalid voucher number!";
                        trace("Displaying: " + dtxtVoucherNo);
                        //pauses the app. for 5 seconds
                        var TimeDelay:Number = setTimeout(MainSlide, 5000, "5 second delay");
                        continue;
                        //then returns the app. to the Main screen
                        // GoTo Screen behavior
                        function MainSlide():Void {
                        //clears the Time Delay
                        clearTimeout(TimeDelay);
                        if((this._parent.Main != undefined) && (this._parent.Main != null))
                        {
                        var screen = null;
                        var target = this;
                        while((screen == null) && (target != undefined) && (target != null))
                        {
                        if(target instanceof mx.screens.Screen)
                        {
                        screen = target;
                        }
                        else
                        {
                        target = target._parent;
                        }
                        }
                        if(screen instanceof mx.screens.Slide)
                        {
                        screen.gotoSlide(this._parent.Main);
                        }
                        // End GoTo Screen behavior
                        }
                        • 9. Re: Dynamic Text
                          zensoldier Level 1
                          Sure thing, try this...

                          setTimeout(MainSlide, 5000)

                          function MainSlide()
                          {
                          trace("it works");
                          }

                          You can cast it the way you have it coded now so you can clear it if you like but I typically use it as written above


                          var TimeDelay:Number = setTimeout(MainSlide, 5000);

                          function MainSlide()
                          {
                          trace("it works");
                          clearTimeout(TimeDelay);
                          }
                          • 10. Re: Dynamic Text
                            zensoldier Level 1
                            I put your code into flash to format it and it looks like you have some syntax issues going on in there.

                            The continue is used in loops so it doesn't hecessarily belong there and it looks like you are missing closing brackets here and there so you end up with a function definition inside of your opening if statement and some other stuff in there.

                            Get your comments out of the way and take a close and the basic structures like opening and closing brackets, get the function out of the if structure and just take a close look at closeing brackets and such.

                            I did this but I'm not sure if its the functionality you are after.


                            if (VoucherNo.length < 14)
                            {
                            this.dtxtVoucherNo.text = "invalid voucher number!";
                            trace("Displaying: " + dtxtVoucherNo);

                            var TimeDelay:Number = setTimeout(MainSlide, 5000, "5 second delay");

                            if(screen instanceof mx.screens.Slide)
                            {
                            screen.gotoSlide(this._parent.Main);
                            }
                            }

                            function MainSlide():Void
                            {
                            clearTimeout(TimeDelay);
                            if((this._parent.Main != undefined) && (this._parent.Main != null))
                            {
                            var screen = null;
                            var target = this;
                            while((screen == null) && (target != undefined) && (target != null))
                            {
                            if(target instanceof mx.screens.Screen)
                            {
                            screen = target;
                            }
                            else
                            {
                            target = target._parent;
                            }
                            }
                            }
                            }
                            • 11. Re: Dynamic Text
                              Wolf_van_Ween Level 1
                              No, G0001 you didn't understand me:
                              *everything* you want to do only after the message box has been displayed for 10 seconds needs to go into the changedtxt function, not only the text for the error message, also the gotoSlide etc. Everything.

                              The "continue" I've written was pseudo-code, you don't really write this into your actionscript. It was a placeholder for anything else your flash can do in the meantime, while it waits for the 10 seconds.

                              So, maybe this is more clear, again the structure:

                              do things;
                              myInterval = setInterval("doItAll",10000);
                              display error message;
                              do things that can happen in the meantime, e.g. preload content

                              function doItAll():Void {
                              do everything now that has to wait, e.g move to next slide
                              delete myInterval;
                              }
                              • 12. Re: Dynamic Text
                                Goo101 Level 1
                                Thanks guys . . . I did a small test app. to check the setTimeout logic and it works fine but in my real app. the code is not entering the function logic after the time delay . . . this is infuriating!

                                My full (uncommented) code is as follows . . .

                                if (VoucherNo.length < 14) {
                                this.dtxtVoucherNo.text = "invalid voucher number!";
                                setTimeout(MainSlide, 5000);
                                function MainSlide() {
                                if((this._parent.Main != undefined) && (this._parent.Main != null))
                                {
                                var screen = null;
                                var target = this;
                                while((screen == null) && (target != undefined) && (target != null))
                                {
                                if(target instanceof mx.screens.Screen)
                                {
                                screen = target;
                                }
                                else
                                {
                                target = target._parent;
                                }
                                }
                                if(screen instanceof mx.screens.Slide)
                                {
                                screen.gotoSlide(this._parent.Main);
                                }
                                clearTimeout(TimeDelay);
                                }
                                else if (VoucherNo > this._parent.Connect_Check.LastGoodVoucher and VoucherNo < upperVoucher) {
                                if((this._parent.Format != undefined) && (this._parent.Format != null))
                                {
                                var screen = null;
                                var target = this;
                                while((screen == null) && (target != undefined) && (target != null))
                                {
                                if(target instanceof mx.screens.Screen)
                                {
                                screen = target;
                                }
                                else
                                {
                                target = target._parent;
                                }
                                }
                                if(screen instanceof mx.screens.Slide)
                                {
                                screen.gotoSlide(this._parent.Format);
                                }
                                }
                                }
                                else {
                                this.dtxtVoucherNo.text = "invalid voucher number!";
                                }
                                }
                                }

                                The code is displaying the error message but nothing else is happening, I don't understand this, I really don't. Everything looks good to me and it's not generating any syntax error upon compile or anything like that.

                                Please someone tell me what's wrong with this before my head explodes!?
                                • 13. Re: Dynamic Text
                                  Goo101 Level 1
                                  On further inspection, it looks like the code is either not recognising or not executing (same difference!) the MainSlide function code . . . why would this be? I know the code . . .

                                  setTimeout(MainSlide, 5000);

                                  . . . is like a function call so I don't understand why it's not recognising the function . . .
                                  • 14. Re: Dynamic Text
                                    Goo101 Level 1
                                    The error in the app. (in that it was not recognising or executing the MainSlide function) was probably something to do with the fact that the function was being defined after it was being called and was therefore undefined when it was being called . . . at least I think is what the problem was . . . I'm unsure as it's still not working . . .
                                    • 15. Re: Dynamic Text
                                      Goo101 Level 1
                                      is the setTimeout command native Flash functionality or not? it doesn't highlight in blue text as other reserved keywords/functions do in Flash, why not? if it's Javascript do I have to specify it as such in my Flash code in order for it to work?
                                      • 16. Re: Dynamic Text
                                        Goo101 Level 1
                                        Ok, this is the final code logic and it all works(!), except for the setTimeout logic (which should work as it's been coded correctly but it doesn't) . . .

                                        //Define the MainSlide function . . .
                                        function MainSlide():Void {
                                        // GoTo Screen behavior
                                        if((this._parent.Main != undefined) && (this._parent.Main != null))
                                        {
                                        var screen = null;
                                        var target = this;
                                        while((screen == null) && (target != undefined) && (target != null))
                                        {
                                        if(target instanceof mx.screens.Screen)
                                        {
                                        screen = target;
                                        }
                                        else
                                        {
                                        target = target._parent;
                                        }
                                        }
                                        if(screen instanceof mx.screens.Slide)
                                        {
                                        screen.gotoSlide(this._parent.Main);
                                        }
                                        // End GoTo Screen behavior
                                        }
                                        }

                                        //Then use it in the if statement logic . . .

                                        // Checks the validity of VoucherNo based to the last good known Voucher Number
                                        // and upper Voucher range (based on tomorrow's date)
                                        if (itxtVoucherNo.length < 14) {
                                        //changes the dynamic text field to display an "Invalid Voucher Number" message
                                        this.dtxtVoucherNo.text = "invalid voucher number!";
                                        //calls the MainSlide function to return the app. to the Main screen after a 5
                                        //second delay
                                        setTimeout(MainSlide, 5000);
                                        // MainSlide();
                                        // clearTimeout;
                                        }
                                        else if (VoucherNo > this._parent.Connect_Check.LastGoodVoucher and VoucherNo < upperVoucher) {
                                        //if Voucher Number is valid the app. goes to the next screen
                                        // GoTo Screen behavior
                                        if((this._parent.Format != undefined) && (this._parent.Format != null))
                                        {
                                        var screen = null;
                                        var target = this;
                                        while((screen == null) && (target != undefined) && (target != null))
                                        {
                                        if(target instanceof mx.screens.Screen)
                                        {
                                        screen = target;
                                        }
                                        else
                                        {
                                        target = target._parent;
                                        }
                                        }
                                        if(screen instanceof mx.screens.Slide)
                                        {
                                        screen.gotoSlide(this._parent.Format);
                                        }
                                        }
                                        // End GoTo Screen behavior
                                        trace("Voucher Number is valid!");
                                        }
                                        else {
                                        //changes the dynamic text field to display an "Invalid Voucher Number" message
                                        this.dtxtVoucherNo.text = "invalid voucher number!";
                                        //calls the MainSlide function to return the app. to the Main screen after a 5
                                        //second delay
                                        setTimeout(MainSlide, 5000);
                                        }

                                        If you notice that there is a line of commented out code which simply calls the MainSlide function without the 5 second delay. This code works when uncommented. Yet it won't work when the setTimeout (5 second delay logic) is placed around the function call.

                                        Why is this? Logically this code should work just as well as a simple call to the MainSlide function but it's not. I'm totally lost as to explain why this is the case!!!

                                        • 17. Re: Dynamic Text
                                          Goo101 Level 1
                                          All,
                                          I managed to get this sorted using the setInterval function! My problem was that I wasn't setting the scope of the MainSlide function correctly. I corrected this with the following code . . .

                                          myInterval = setInterval(this, "MainSlide", 5000);

                                          Thanks everyone for their help, 'tis much appreciated.
                                          Good luck!
                                          • 18. Re: Dynamic Text
                                            Goo101 Level 1
                                            I also managed to get the setTimeout function working, again by specifying the scope of the MainSlide function, as per the following code . . .

                                            myTimeout = setTimeout(this, "MainSlide", 5000);

                                            You then place the following line of code inside your function (e.g. MainSlide) to clear the timeout . . .

                                            clearTimeout(myTimeout);

                                            Hopefully, this helps out anyone who is experiencing a similar problem!