1

My program that I am making is tetris. One part I got stuck on was decreasing the speed in the timer when the player reaches a certain score. For instance, every 200 points (modulo will check) will decrease the time lets say by 50. Initially the time started at 500 ms. I can't seen to figure out how to decrease it because once I created the Timer, the time that is passed in doesn't change.

** My constructor which instantiates the timer.

public PlayingPanel(SidePanel p)
{
  pf = new PlayingField(p);
  generateNewPiece(); 
  setFocusable(true);
  addKeyListener(new KeyEvent());
  timer = new Timer(time, new BlockListener());
  timer.start();   
}

public void startTimer()
{
  time -= 20;
}

/**
 * generates new piece
 */
public void generateNewPiece()
{
  startTimer();
  ...
}
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
Java investor
  • 117
  • 3
  • 12

4 Answers4

3

Timer.setDelay(int ms)

Sets the Timer's between-event delay, the number of milliseconds between successive action events. This does not affect the initial delay property, which can be set by the setInitialDelay method.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
2

By default, javax.swing.Timer will coalesce events; you may be seeing this effect. You can self-time your animations, as shown here, to verify that you're not exceeding the available animation budget at highest speed (shortest period).

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I'm still a beginner but what i'm trying to do is just update the time. I just can't seem to figure out how to update it. I can change the value for it, but I can't get it to go into the Timer object to decrease the speed. – Java investor Apr 19 '12 at 01:11
  • This [example](http://stackoverflow.com/a/5048863/230513) uses `setDelay()` to vary the speed, as @Greg Kopf suggests. – trashgod Apr 19 '12 at 01:17
0

The timer object will never get your changes to the time variable. You can cancel the current timer and create a new one, with -20 as an argument and update your events with the new timer. My Swing Java-fu is weak, but in 1.6 you may be able to call setDelay to update the existing timer with a new time, in ms.

Nick Martin
  • 282
  • 1
  • 4
0

You can create a class, say GameTimer that handles all the time-based events. GameTimer will register itself to a Timer, or will use whatever time measuring method you choose. Your other objects can then register themselves to GameTimer (maybe with their desired frequency) and GameTimer will notify them accordingly. This way you abstract yourself from Timer, which is generally a good idea, and you have better control of how time-based events are handled. If GameTimer registers to a timer and tells it to update at 10ms, that should be enough precision for up to 100fps. You can then have GameTimer notify BlockListener every-whatever measure you want.

That also means that if you ever find a better way to measure the time, you won't have to change code in multiple places.

elite5472
  • 2,184
  • 4
  • 21
  • 28