3 Replies Latest reply: Apr 25, 2012 6:22 AM by kglad RSS

    Random No. Generator

    Bawlar

      Hello,

       

      I am new to Flash and AS3 and I was trying to create a random number generator. I've included the code at the end. I have a button to generate the number and a dynamic text box called 'output' in which to display the number.

       

      However, when I click "generate", I get the error message --> "1067: Implicit coercion of a value of type uint to an unrelated type String"

       

      I assume this means Flash can't display the number as a string, but is there someway I can do this? Or something I've left out? Can you convert one data type to another? I know you can cast String into numbers, can you do it the other way?

       

      Thanks for any help

       

      CODE:

       

      package

      {

          import flash.display.MovieClip

          import flash.events.MouseEvent

         

          public class Main extends MovieClip

          {

              public function Main()

              {

                  generateButton.addEventListener(MouseEvent.CLICK, onGenerateButtonClick);

              }

              function onGenerateButtonClick(event:MouseEvent):void

              {

                  var randNum:uint = Math.ceil(Math.random() * 100);

                  output_txt.text = randNum;

              }

          }

      }

        • 1. Re: Random No. Generator
          kglad MVP

          you can cast randNum as a string or use:

           

           

           

          package

          {

              import flash.display.MovieClip

              import flash.events.MouseEvent

           

              public class Main extends MovieClip

              {

                  public function Main()

                  {

                      generateButton.addEventListener(MouseEvent.CLICK, onGenerateButtonClick);

                  }

                  function onGenerateButtonClick(event:MouseEvent):void

                  {

                      var randNum:uint = Math.ceil(Math.random() * 100);

                     output_txt.text = randNum.toString();

                  }

              }

          }

          • 2. Re: Random No. Generator
            Bawlar Community Member

            Thank you, works like a charm .

            • 3. Re: Random No. Generator
              kglad MVP

              you're welcome.