Hello peoples.
I was wondering if someone could tell me if this was a good idea of how to do animation and other time based tasks.
Its a work around I made so I would not have to use Threads to time things.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| public class RateManager { private int currentUpdate; private int rate; private int mainRate; public RateManager( int newRate, int newMainRate ) { this.rate = newRate / newMainRate; this.mainRate = newMainRate; this.currentUpdate = 0; } public boolean update() { if( this.currentUpdate == this.rate ) { this.currentUpdate = 0; return true; } else { this.currentUpdate++; return false; } } public void setRate( int newRate ) { this.rate = newRate / this.mainRate; } public void setRate( int newRate, int newMainRate ) { this.rate = newRate / newMainRate; this.mainRate = newMainRate; }
}
|
Basically, if you were updating a loop with Thread.sleep(someTime)
you could animate/time/whatever by having an object that you create that would get the amount of updates
it would take for it to "update" based on what speed you're main loop is running at.
Lets say we are sleeping our loop every 33mils so about 30fps and we want something to shoot off every 280mils.
Then we create a object that will see how many times the main loop needs to update before it would shoot off.
So we update the object and it will return whether it has shot off or not.