'Variables to set speed of something moving won't work when using action listeners
I am trying to change the speed of an item in a game (Tetris) and I am making a logic error that means when the key is pressed, the speed of the falling shape doesn't change. The error has something to do with the variables normalSpeed and fastSpeed as they're currently set to 600 and 50 but even when I change the values the speed remains the same.
Would you be able to see where I am going wrong, please:)
public class gamePanel extends JPanel implements KeyListener {
private static int framesPerSec = 60;
private static int delayForFrames = 1000 / framesPerSec;
public static final int boardWidth = 10;
public static final int boardHeight = 20;
public static final int pieceSize = 30;
private Timer looper;
private Color[][] board = new Color[boardWidth][boardHeight];
private Color[][] shape = {
{Color.RED, Color.RED, Color.RED},
{null, Color.RED, null}
};
private int x = 4, y = 0;
private int normalSpeed = 600;
private int fastSpeed = 50;
private int delayForShape = normalSpeed;
private long beginTime;
public gamePanel() {
// this lets the shape fall down the screen
looper = new Timer(delayForFrames, new ActionListener() {
int n =0;
@Override
public void actionPerformed(ActionEvent e) {
if(System.currentTimeMillis() - beginTime > delayForShape){
y++;
beginTime = System.currentTimeMillis();
}
repaint();
}
});
looper.start();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
