This site requires JavaScript, please enable it in your browser!
Greenfoot back
lonely.girl
lonely.girl wrote ...

2012/12/12

explanination

lonely.girl lonely.girl

2012/12/12

#
Can anybody plz explain this part of code for me? I understand that it's the delay for the loop but what does >0 then delay -- means ??? I wrote a comment for the programmer here also: music is fun private void delay() { if (delay > 0) { delay--; } if (delay == 0) { play(); delay = 15; } }
lonely.girl lonely.girl

2012/12/12

#
sorry typo, meant explanation :/
vonmeth vonmeth

2012/12/12

#
 private void delay()
    {
        if (delay > 0) // is delay GREATER THAN 0
        {    
            delay--; // subtract 1 from delay
        }
        if (delay == 0) // is delay EQUAL TO 0
        {
            play(); // run play method
            delay = 15; // set delay to 15
        }
    }
It is a way to make it so that play() can not be called over and over.
danpost danpost

2012/12/12

#
Yes, it runs a delay for the method 'play'. First, it asks if the delay is running (if the value of delay is greater than zero) If it is, then the value is decremented. Now, it asks if the delay count is exhausted. If so, then execute the 'play' method and restart the delay count (back to 15). It appears that the delay will always be running, unless the value of 'delay' is set to a negative number. I would have probably coded it slightly different
private void delay()
{
    if(delay<0)return; // if delay not running, exit method
    delay=(delay+1)%15 // wrap-cycle the delay count
    if(delay==0) play(); // execute the method when delay count completed
}
The direction the counter goes (up or down) does not make much difference, as the method is still executed every 15 cycles (when the delay is running).
lonely.girl lonely.girl

2012/12/12

#
@vonmeth Thanks for your help @danpost Thanks a lot, I think I'll use the code you provided if it's ok because I don't understand (delay--) with the dashes thingies :/
danpost danpost

2012/12/12

#
The 'delay--;' is shorthand for 'delay = delay - 1;' or 'delay -= 1;' (they all do the same thing. Same with 'delay = delay + 1;', 'delay += 1;' and 'delay++;'.
You need to login to post a reply.