Skip to main content
Participant
July 28, 2015
Answered

mouse click generate number

  • July 28, 2015
  • 2 replies
  • 576 views

I'm trying to make a mouse event that return a random number to a dynamic text box...

when i publish the function that generates the number works

but when i call the function with the mouse click it doesn't work.

Could someone help me?

Tnks!!!

var resultado:Number=sorteio();

function sorteio():Number

{

return Math.round(Math.random() *500);

}

sorteio();

sorteio_txt.text = String(resultado);

my_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void

{

     sorteio();

}

This topic has been closed for replies.
Correct answer Elliot Mebane

To change your original code the least you could replace the sortio function with this. It assigns the new random number to the resultado variable then assigns resultado to the text field.

function sorteio():Number

{

  resultado = Math.round(Math.random() *500);

  sorteio_txt.text = resultado.toString();

  return resultado;

}

kglad's solution is also great and works.

In your example you want a string version of the random number. A good design would keep your resultado variable saved as a number and convert it to a string only when you need to display it in a text field. In this version you have resultado saved as a number variable and each method does its thing. Everything is a little more decoupled.

var resultado:Number;

 

sorteio();

displayResultado();

my_btn.addEventListener(MouseEvent.CLICK, onClick);

 

function sorteio():void

{

  resultado = Math.round(Math.random() *500);

}

function displayResultado():void

{

  sorteio_txt.text = resultado.toString();

}

function onClick(e:MouseEvent):void

{

  sorteio();

  displayResultado();

}

2 replies

Participant
July 29, 2015

‌thank you guys for The different aproaches To The problem!

it helped me a lot To understand differents Ways of dealing with it!

kglad
Community Expert
Community Expert
July 28, 2015

use:

function sorteio():String

{

return Math.round(Math.random() *500).toString();

}

sorteio_txt.text = sorteio();

my_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void

{

  sorteio_txt.text = sorteio();

}

Elliot MebaneCorrect answer
Participating Frequently
July 28, 2015

To change your original code the least you could replace the sortio function with this. It assigns the new random number to the resultado variable then assigns resultado to the text field.

function sorteio():Number

{

  resultado = Math.round(Math.random() *500);

  sorteio_txt.text = resultado.toString();

  return resultado;

}

kglad's solution is also great and works.

In your example you want a string version of the random number. A good design would keep your resultado variable saved as a number and convert it to a string only when you need to display it in a text field. In this version you have resultado saved as a number variable and each method does its thing. Everything is a little more decoupled.

var resultado:Number;

 

sorteio();

displayResultado();

my_btn.addEventListener(MouseEvent.CLICK, onClick);

 

function sorteio():void

{

  resultado = Math.round(Math.random() *500);

}

function displayResultado():void

{

  sorteio_txt.text = resultado.toString();

}

function onClick(e:MouseEvent):void

{

  sorteio();

  displayResultado();

}