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

2012/3/23

For loop

UKPowerMax UKPowerMax

2012/3/23

#
Hello, I am trying to use a for loop in order for my games's timer to be added to my games score. I want this to happen when my player wins and have the remaining time on their clock added as points to the score counter. This is what my current loop code looks like and I'm not sure why it is not working because I am relatively new to java. public void pointloop() { for(int i=0; i<timer; i++){ ((Background) getWorld()).timertocounter(); } } Thanks for any help guys!
danpost danpost

2012/3/23

#
Going through a loop as you are here is essentially the same as 'score += timer;'. If you are trying to visually show the timer points being added to the counter one by one, then you need an 'if', not a 'for'.
danpost danpost

2012/3/23

#
Going through a loop as you are here is essentially the same as 'score += timer;'. If you are trying to visually show the timer points being added to the counter one by one, then you need an 'if', not a 'for'.
if (gameOver() && timer > 0)
{
    timer--;
    score++;
    delay(10);
}
where gameOver() would either be a method you create returning a true/false value depending on the condition that should be met to constitute that the game is over, or the conditions themselves. Also, I abbreviated the code within the curly brackets (you need to adjust them to fit your scenario). The 'delay' was added so that the score did not go up so fast that you could not actually see it happening.
danpost danpost

2012/3/23

#
Sorry for the multiple partial postings (I accidentally tried to use the tab key in the posting and that selects the 'Post' button, where I must have hit 'Enter' a couple of times). I forgot to mention that the code above should be in an act() method or in a method that an act() method calls.
You need to login to post a reply.