Skip to main content
January 4, 2010
Answered

how to check, how many times the modulo % start over.

  • January 4, 2010
  • 2 replies
  • 670 views

Hello,

Suppose you have an enterFrame that increases a value:

In this case the speed is increased by 4 every times.

my_value 4

my_value 8

my_value 12

my_value 16

my_value 20

my_value 24

my_value 28

my_value 32

my_value 36

my_value 40

my_value 44

my_value 48

my_value 52

this value is stored in a variable called my_value;

If you want to have a maximum and that the result values starts at 0 again, you can use the modulo %
In my example the maximum is 50. So i trace out (my_value%50) and see that it start over and does not
go over the 50. But how can i count how many times it reaches it's  maximum?

[as]

//the code:

var my_value = 0;

var my_speed = 4;

var countloops = 0;

this.onEnterFrame = function() {

my_value += my_speed;

doSomething(my_value);

}

function doSomething(myvalue) {

var maxValue = myvalue%50;

if(maxValue>=50) {

trace("loop");

countloops++;

}

trace("maxValue:" + maxValue);

}

[/as]

Does anyone has a good idea to count the loops?

thanks!

Regards,
Chris.

This topic has been closed for replies.
Correct answer Ned Murphy

What you'll want to do is have a currentValue that you compare against the checkValue.  The currentValue starts at zero and only changes when the checkValue does not equal it.  If they are equal then you don't call updateLoop function.... if they are not equal, that means the checkValue just changed again so you then want to call the updateLoop function and adjust the currentValue to once again equal the checkValue.

2 replies

kglad
Community Expert
Community Expert
January 4, 2010

use:

Math.floor(my_value/50);

January 5, 2010

Yes, i haven't been clear on what i am trying to do, but the only thing i am trying to accomplish is,
to run a function when the module starts over.
Thanks to the single line of Kglad i now got it working, but now every onEnterframe the loop function is called.
Is it also possible to run the  updateLoop() function only when it changes, if there is a new value?

This is the code i have so far:

var my_value = 0;

var my_speed = 4;

var countloops = 0;

this.onEnterFrame = function() {

my_value += my_speed;

doSomething(my_value);

}

function doSomething(myvalue) {

var maxValue = myvalue%50;

//to calculate how many loops there are going.

var checkvalue = Math.floor(my_value/50);

if(checkvalue == 1) {

updateLoop(checkvalue);

}

if(checkvalue == 2) {

updateLoop(checkvalue);

}

if(checkvalue == 3) {

updateLoop(checkvalue);

}

if(checkvalue == 4) {

updateLoop(checkvalue);

}

//and so on.

}

function updateLoop(loops) {

trace("this is loop number " + loops);

}

Ned Murphy
Ned MurphyCorrect answer
Legend
January 5, 2010

What you'll want to do is have a currentValue that you compare against the checkValue.  The currentValue starts at zero and only changes when the checkValue does not equal it.  If they are equal then you don't call updateLoop function.... if they are not equal, that means the checkValue just changed again so you then want to call the updateLoop function and adjust the currentValue to once again equal the checkValue.

Ned Murphy
Legend
January 4, 2010

I'm not sure what you are trying to do, so you may have to explain that more clearly.  The value of my_value is constantly growing, well beyond 50, and the conditional... maxValue>=50 will never be satisfied.  Due to the modulo operation there can never be a maxValue that is >= 50... the modulo returns the remainder after dividing by 50, which will always be < 50