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

How to repeat the last random number

Contributor ,
Dec 04, 2015 Dec 04, 2015

Hello to everyone!

I have a timer and a random number (var myNumber: Number = Math.ceil(Math.random() * 5);).

I need to repeat the last random number, eg: 3 3, 4 4, 1 1, etc.

Any suggestions?

Thanks.

TOPICS
ActionScript
762
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

correct answers 1 Correct answer

Community Expert , Dec 04, 2015 Dec 04, 2015

use:

kglad wrote:

use:

 var myNum: Number;
  •   var mySound: Sound = new Click();
  •   var mySound2: Sound = new Sound2();
  •   var fl_TimerInstance: Timer = new Timer(1000, 30);
  •   fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
  1. Count_It.addEventListener(MouseEvent.CLICK, Count_It);
  2. function Contalo(event: MouseEvent): void {
  3. fl_TimerInstance.reset();
  4.   fl_TimerInstance.start();
  5. }
  •   function fl_TimerHandler(event: TimerEvent): void {
  •   if (event.currentCount % 2==1) {
  •   myN
...
Translate
Community Expert ,
Dec 04, 2015 Dec 04, 2015

use the myNumber reference without re-executing that line of code you showed.

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
Contributor ,
Dec 04, 2015 Dec 04, 2015

Thank you very much kglad.

I think it is better to see all the script.

I'm trying to repeat the random sequence switching between odd and even.

The only think that doesn't work is the line 16 that give "NaN"

Any hint?

  1. Count_It.addEventListener(MouseEvent.CLICK, Count_It);
  2. function Contalo(event: MouseEvent): void {
  3.   var mySound: Sound = new Click();
  4.   var mySound2: Sound = new Sound2();
  5.   var fl_TimerInstance: Timer = new Timer(1000, 30);
  6.   fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
  7.   fl_TimerInstance.start();
  8.   var fl_SecondsElapsed: Number = 1;
  9.   function fl_TimerHandler(event: TimerEvent): void {
  10.   trace("Second elapsed i: " + fl_SecondsElapsed);
  11.   fl_SecondsElapsed++;
  12.   if (fl_SecondsElapsed % 2) {
  13.   var myNum: Number = Math.ceil(Math.random() * 8);
  14.   trace(myNum);
  15.   mySound.play();
  16.   } else {
  17.   mySound2.play();
  18.   trace(myNum);
  19.   }
  20.   }
  21. }
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
Community Expert ,
Dec 04, 2015 Dec 04, 2015

i can't determine what you're trying to do, but you should never nest named functions and myNum serves no (shown or explained) purpose.

 var myNum: Number
  •   var mySound: Sound = new Click();
  •   var mySound2: Sound = new Sound2();
  •   var fl_TimerInstance: Timer = new Timer(1000, 30);
  •   fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);

  1. Count_It.addEventListener(MouseEvent.CLICK, Count_It);
  2. function Contalo(event: MouseEvent): void {
  3. fl_TimerInstance.reset();
  4.   fl_TimerInstance.start();
  5. }
  •   function fl_TimerHandler(event: TimerEvent): void {
  •   if (event.currentCount % 2) {
  •   myNum = Math.ceil(Math.random() * 8);
  •   trace(myNum);
  •   mySound.play();
  •   } else {
  •   mySound2.play();
  •   trace(myNum);
  •   }
  •   }
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
Contributor ,
Dec 04, 2015 Dec 04, 2015

Thank you kglad!

I'm trying the purpose of my first question.

1) to have a counter;

2) for the first second to get a random number;

3) for the next second to have te same random number (without use Math.random), and so on ...

I try to do this with odd and even numbers;

1) If the number of the counter is odd I create a random number;

2) "else" if the number is even, I repeat the same random number, but in my script doesn't work ...

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
Community Expert ,
Dec 04, 2015 Dec 04, 2015

use:

 var myNum_odd: Number;
 var myNum_even: Number
  •   var mySound: Sound = new Click();
  •   var mySound2: Sound = new Sound2();
  •   var fl_TimerInstance: Timer = new Timer(1000, 30);
  •   fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
  
  1. Count_It.addEventListener(MouseEvent.CLICK, Count_It);
  2. function Contalo(event: MouseEvent): void {
  3.   myNum_odd = Math.ceil(Math.random() * 8);
  4.   myNum_even = Math.ceil(Math.random() * 8);
  5. fl_TimerInstance.reset();
  6.   fl_TimerInstance.start();
  7. }
  •   function fl_TimerHandler(event: TimerEvent): void {
  •   if (event.currentCount % 2) {
  •   trace(myNum_odd);
  •   mySound.play();
  •   } else {
  •   mySound2.play();
  •   trace(myNum_even);
  •   }
  •   }
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
Contributor ,
Dec 04, 2015 Dec 04, 2015

Thank you again kglad.

With your script I get this error message:

Scene 1 level 'Actions', Frame 1, line 16, column 12, 1119: Access to an undefined property currentCount through a reference with static type flash.events:TimerEvent.

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
Community Expert ,
Dec 04, 2015 Dec 04, 2015

my error, that should be

event.currentTarget.currentCount,

not event.currentCount

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
Contributor ,
Dec 04, 2015 Dec 04, 2015

You are very kind and  now the script work better, and I suppose to be to one step from  my purpose.

The script now give me, in the output window with the trace function, something like this:

1

3

1

3

1

3

and so on for the 30 steps of the timer.

But my purpose was to have, with only one click, for the 30 steps of the timer, something like this:

3

3

//random

5

5

//random

2

2

and so on for the 30 steps of the timer.

Every number must be at distance of one second (1000 in the timer): like this 3 3, 5 5 etc.

I'm sorry but it is my fault. I don't have explain well it from my first message.

Thank you again.

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
Community Expert ,
Dec 04, 2015 Dec 04, 2015

use:

kglad wrote:

use:

 var myNum: Number;
  •   var mySound: Sound = new Click();
  •   var mySound2: Sound = new Sound2();
  •   var fl_TimerInstance: Timer = new Timer(1000, 30);
  •   fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
  1. Count_It.addEventListener(MouseEvent.CLICK, Count_It);
  2. function Contalo(event: MouseEvent): void {
  3. fl_TimerInstance.reset();
  4.   fl_TimerInstance.start();
  5. }
  •   function fl_TimerHandler(event: TimerEvent): void {
  •   if (event.currentCount % 2==1) {
  •   myNum = Math.ceil(Math.random() * 8);
  •   trace(myNum);
  •   mySound.play();
  •   } else {
  •   mySound2.play();
  •   trace(myNum);
  •   }
  •   }
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
Contributor ,
Dec 04, 2015 Dec 04, 2015

Thank you so much kglad!!!!

You are my hero

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
Community Expert ,
Dec 04, 2015 Dec 04, 2015
LATEST

you're welcome.

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