5 Replies Latest reply on Jan 7, 2010 12:41 AM by chrisatflash

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

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.

• ###### 1. Re: how to check, how many times the modulo % start over.

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

• ###### 2. Re: how to check, how many times the modulo % start over.

use:

Math.floor(my_value/50);

• ###### 3. Re: how to check, how many times the modulo % start over.

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

}

• ###### 4. Re: how to check, how many times the modulo % start over.

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.

• ###### 5. Re: how to check, how many times the modulo % start over.

Brilliant Ned! Thanks, i got it working now. This is the code and it's a lot smaller

var my_value = 0;

var my_speed = 4;

var countloops = 0;

this.onEnterFrame = function() {

my_value += my_speed;

doSomething(my_value);

}

var checkvalue = 0;

function doSomething(myvalue) {

if(checkvalue != Math.floor(my_value/50)) {

updateLoop(checkvalue);

}

//to calculate how many loops there are going.

checkvalue = Math.floor(my_value/50);

}

function updateLoop(loops) {

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

}