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

mouse click generate number

New Here ,
Jul 28, 2015 Jul 28, 2015

Copy link to clipboard

Copied

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

}

TOPICS
ActionScript

Views

393

Translate

Translate

Report

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

correct answers 1 Correct answer

Explorer , Jul 28, 2015 Jul 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 numbe

...

Votes

Translate

Translate
Community Expert ,
Jul 28, 2015 Jul 28, 2015

Copy link to clipboard

Copied

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

}

Votes

Translate

Translate

Report

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
Explorer ,
Jul 28, 2015 Jul 28, 2015

Copy link to clipboard

Copied

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

}

Votes

Translate

Translate

Report

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 ,
Jul 28, 2015 Jul 28, 2015

Copy link to clipboard

Copied

LATEST

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

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

Votes

Translate

Translate

Report

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