What timer should i use for Tetris?

Hi. I've tried a few times to make my own tetris.

The problem is that when i move manually sometimes it respond correctly and sometimes it does not.

When the timer is off it works always right.

I've tried both util and swing timer.

I've had the same problem with other timered-games.

Any suggestion?

Thank you

[355 byte] By [Zykera] at [2007-11-27 6:16:03]
# 1
If you could, please include some relevant code to help us with solving your problem. Use the code /code tags by clicking the "code" button.
tk393a at 2007-7-12 17:27:21 > top of Java-index,Java Essentials,Java Programming...
# 2

The code is a little long.

This is the TimerTask

import java.util.*;

class GestoreTimer extends TimerTask {

MappaTetris corrente;

public GestoreTimer(MappaTetris mappa) {

this.corrente = mappa;

}

public void setMappa(MappaTetris mappa) {

this.corrente = mappa;

}

public void run() {

MappaTetris.creaInterfaccia(Muovi.muovi(corrente,"BASSO"));

}

}

which calls

public static void creaInterfaccia(MappaTetris mappa) {

Tetris.finestra.getContentPane().removeAll();

mappa = RimuoviLinea.rimuoviLinee(mappa);

JPanel pannelloComandi = new JPanel(new GridLayout(2,1));

pannelloComandi.add(inizia());

pannelloComandi.add(pausa(mappa));

JPanel pannelloMappa = pannelloMappa(mappa);

JPanel pannelloPunteggio = Tetris.pannelloPunteggio();

JPanel pannelloFigura = Tetris.figuraSuccessiva.pannelloFigura();

JPanel pannelloDestro = new JPanel(new BorderLayout());

pannelloDestro.add(pannelloFigura,BorderLayout.NORTH);

pannelloDestro.add(pannelloPunteggio,BorderLayout.CENTER);

pannelloDestro.add(pannelloComandi,BorderLayout.SOUTH);

Tetris.tt.setMappa(mappa);

Tetris.ascoltaTastiera.setMappa(mappa);

pannelloMappa.addKeyListener(Tetris.ascoltaTastiera);

Tetris.finestra.getContentPane().add(pannelloMappa,BorderLayout.CENTER);

Tetris.finestra.getContentPane().add(pannelloDestro,BorderLayout.EAST);

Tetris.finestra.pack();

pannelloMappa.requestFocusInWindow();

Tetris.finestra.setVisible(true);

}

and this is for moving

public static MappaTetris muovi(MappaTetris mappa, String direzione) {

MappaTetris mappaPonte = mappa;

int possibile = mossaPossibile(mappa, direzione);

if(possibile == 0) {

System.out.println("POSSIBILE = 0");

mappa = MappaTetris.stacca(mappa);

String[][] mappaSolida = mappa.getMappaSolida();

Figura figuraNuova = mappa.getFigura();

Punto puntoIniziale = figuraNuova.getPosizione();

Punto puntoDirezione;

if(direzione.equals("DESTRA"))

puntoDirezione = new Punto(0,1);

else if(direzione.equals("SINISTRA"))

puntoDirezione = new Punto(0,-1);

else

puntoDirezione = new Punto(1,0);

Punto puntoFinale = Punto.somma(puntoIniziale,puntoDirezione);

figuraNuova.setPosizione(puntoFinale);

MappaTetris mappaNuova = new MappaTetris(mappaSolida, figuraNuova);

return mappaNuova;

}

else if(possibile == 1) {

System.out.println("POSSIBILE = 1");

MappaTetris daRestituire = MappaTetris.mappaConFigura(mappaPonte, Tetris.figuraSuccessiva);

return daRestituire;

}

else if(possibile == 2) {

Tetris.tt.cancel();

System.out.println("POSSIBILE = 2");

return mappaPonte;

}

else {

System.out.println("POSSIBILE = -1");

}

return mappaPonte;

}

}

what else should i post?

thanx

Zykera at 2007-7-12 17:27:21 > top of Java-index,Java Essentials,Java Programming...
# 3

You use a Timer to schedule the dropping of the pieces at your specified interval.

You use Key Bindings to manually move the pieces using the Keyboard. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url] for more information.

camickra at 2007-7-12 17:27:21 > top of Java-index,Java Essentials,Java Programming...