Temporizador giratório - tempo flutuando
Estou usando um temporizador de oscilação no meu jogo, mas quando o jogo está em execução, parece haver momentos em que ocorre sem problemas e momentos em que diminui a velocidad
Por que o tempo está flutuando?
E como faço para corrigir isso?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main extends JFrame {
public Main() {
super("JFrame");
// you can set the content pane of the frame
// to your custom class.
setContentPane(new ImagePanel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 400);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
class ImagePanel extends JPanel {
Timer movementtimer;
int x, y;
public ImagePanel() {
x = 0;
y = 0;
movementtimer = new Timer(12, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timstarted = System.currentTimeMillis();
moveImage();
repaint();
long timefinished = System.currentTimeMillis() - timstarted;
System.out.println(timefinished + " to run");
};
});
movementtimer.start();
}
public void moveImage() {
x++;
y++;
if (x > 800) {
x = 0;
}
if (y > 400) {
y = 0;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(0, 0, 800, 400);
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 50);
}
}
}
Aqui está um exemplo do meu código. No meu programa atual, estou desenhando imagens e não apenas um retângulo. Também há muita detecção de colisão e outros pequenos cálculos acontecend
Além disso, aqui está um link para o arquivo Jar do jogo, para que você possa executá-lo e (espero) ver o que quero dizer.http: //dl.dropbox.com/u/8724803/Get%20To%20The%20Chopper%201.3.ja
Obrigad
Tom