0

I am making a Tetris clone and I am using a tick method for the game loop (not sure if this is necessary). I want to increase the y value of the block object by 50 pixels every second or so. Can I add some type of delay in my tick method, or is there an easier way?

public class SquareBlock extends GameObject{

    public SquareBlock(int x, int y, ID id){
        super(x,y,id);
    }

    public void tick(){
        y += 50;
    }

    public void render(Graphics g){
        g.setColor(Color.red);
        g.fillRect(x, y, 50, 50);
        g.fillRect(x, y + 50, 50, 50);
        g.fillRect(x + 50, y, 50, 50);
        g.fillRect(x + 50, y + 50, 50, 50);
    }
}

Thanks

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 2
    @DawoodibnKareem: nope, his is either a Swing or an AWT program given his use of `java.awt.Graphics` and so he should most definitely **not** use a `java.util.Timer` but rather a `javax.swing.Timer`/[Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) instead. – Hovercraft Full Of Eels Jun 12 '17 at 02:32
  • @HovercraftFullOfEels does that mean the tick method I am using is useless or does the timer go in the tick method? – Evan Wotton Jun 12 '17 at 02:34
  • 1
    Evan, no the Timer does not go anywhere inside of SquareBlock, and tick likely should not be changed. The Timer is in your Game class, the class that drives the whole animation. – Hovercraft Full Of Eels Jun 12 '17 at 02:36
  • @Hovercraft good point. I wonder why he's not using JavaFX. Swing has been obsolete for so long now. – Dawood ibn Kareem Jun 12 '17 at 02:44
  • 1
    @DawoodibnKareem: likely he has academic requirements of which he has no control. – Hovercraft Full Of Eels Jun 12 '17 at 02:45
  • @EvanWotton Just use a Swing Timer as Hovercraft suggested. Don't mess around with `sleep` or with busy waits. – Dawood ibn Kareem Jun 12 '17 at 04:14

0 Answers0