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

2016/2/14

Looping with delay

Freekywill Freekywill

2016/2/14

#
I am making an idle game that involves you placing things on tiles to earn money. How would I go about causing something to run for a certain amount of time, and run only once a second without pausing the whole program?
danpost danpost

2016/2/14

#
Just add a timer field to the class and use an 'if' statement to that which you want to control:
1
2
3
4
5
6
7
8
9
// instance field
private int timer;
 
// in act method
timer = (timer+1)%50;
if (timer < 10)
{
    < do something >
}
Line 5 will have the timer increment each act cycle and reset back to zero once it reaches 50. Line 6 uses 10 and the "certain amount of time" which can be altered to fit your needs.
Freekywill Freekywill

2016/2/14

#
I don't think this works, because I have an if statement checking when you click on it, but wouldn't you constantly have to click on it to make it run? Right now the code runs once and stops because it is so longer being clicked on. Edit - I just made a boolean that determines whether or not it should be running when you click. Thanks for the timer advice
You need to login to post a reply.