Skip to main content
Inspiring
April 25, 2012
Answered

Random No. Generator

  • April 25, 2012
  • 1 reply
  • 1108 views

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;

        }

    }

}

This topic has been closed for replies.
Correct answer kglad

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();

        }

    }

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 25, 2012

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();

        }

    }

}

BawlarAuthor
Inspiring
April 25, 2012

Thank you, works like a charm .

kglad
Community Expert
Community Expert
April 25, 2012

you're welcome.