Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Random Number Generator

New Here ,
Oct 11, 2010 Oct 11, 2010

Hi there,

I'm totally new to ActionScript 3 and just trying some very simple things to try to understand how it works in Flash CS5. I have no-one mentoring me so these forums are my first port of call.  I wanted to create a simple random number generator. Very simple. Click on a button (first_50) that displays a random number in a text box (main_balls.text) set to Classic/Dynamic. The script I have is as follows:

first_50.addEventListener(MouseEvent.CLICK, firstFiftyGenerator);

function firstFiftyGenerator(limit:Number):Number

{


var randomNumber:Number = Math.floor(Math.random()*(limit+1));

return randomNumber;

}


main_balls.text = String(firstFiftyGenerator(50));

I get no errors in the compiler and my button works in runtime. I just don't get a number in my text box when I click on it. Can anyone help?

KR,

Th9nker

TOPICS
ActionScript
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 11, 2010 Oct 11, 2010

try embedding fonts for the textbox.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 11, 2010 Oct 11, 2010

I'm a little surprised there is no error message (which there isn't) due to the event listener throwing an event argument versus the function's argument being a Number.  But, anyways, for the code you show, if you have a textfield with the instance name shown, then when you run that code it will fill the textfield with a random value.  But nothing else of that code will cause that value to be replaced.  The button will call the function, but nothing in that function changes what is in the textfield.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 11, 2010 Oct 11, 2010

Event listener doesn't accept number but, well, event as a parameter. You compiler should, actually, throw an error.

Try:

first_50.addEventListener(MouseEvent.CLICK, onButtonClick);

function onButtonClick(e:MouseEvent):void {
     main_balls.text = String(firstFiftyGenerator(50));
}

function firstFiftyGenerator(limit:Number):Number
{
     var randomNumber:Number = Math.floor(Math.random()*(limit+1));
     return randomNumber;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 11, 2010 Oct 11, 2010

Thanks for your replies on this.  I originally grabbed this code from the Code Snippets with the intention of adjusting it to my own needs. It originally displayed a random number in the Output box with the trace() function. I've replaced the script I had with Andrei wrote but it still isn't working. You'll have to forgive my lack of experience here. I've read for two weeks on the concepts of individual elements in AS3 but have no idea how they fit together yet!

Th9nker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 11, 2010 Oct 11, 2010
LATEST

Got this working now with the following format.

function firstFiftyGenerator(limit:Number):Number
{
var randomNumber:Number = Math.floor(Math.random()*(limit+1));
return randomNumber;
}
main_balls.text = String(firstFiftyGenerator(50));

function bonusBallGenerator(limit:Number):Number
{
var randomNumber:Number = Math.floor(Math.random()*(limit+1));
return randomNumber;
}
bonus_balls.text = String(bonusBallGenerator(10));

next_number.addEventListener(MouseEvent.CLICK, nextNumber);
function nextNumber(e:MouseEvent):void {
gotoAndPlay("next");
}

stop();

Made the stupid mistake of having white text on a white background :S Got to remember not to get too caught up in the script and remember what I'm doing with my graphics. In the end, I put my actionscript on frame 10, deleted my original button, added a bonus ball text field and used frame actions with a new button (next_number) to fire the values again and give me a different set of numbers in my text fields. Thanks for all your help. I now have a UK lottery number picking device!

Th9nker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines