I have here a code snippet of my Tetris project.
private class Game implements Runnable {
private int numDropped = -1;
public void setCount(int count){
count++;
}
public int getCount(){
return count;
}
public void run() {
int column = 4, style = Piece.SHAPES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
while (onPlay) {
if (piece != null) {
if (piece.isAlive()) {
try {
Thread.currentThread().sleep(100);
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
continue;
}
}
checkFullLine();
if (isGameOver()) {
playItem.setEnabled(true);
pauseItem.setEnabled(true);
resumeItem.setEnabled(false);
rightPanel.setPlayButtonEnable(true);
rightPanel.setPauseButtonLabel(true);
displayGameOver();
return;
}
piece = new Piece(style, -1, column, board);
piece.start();
style = Piece.SHAPES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
rightPanel.setTipStyle(style);
numDropped = numDropped+1;
RightPanel.scoreTextField.setText(numDropped+"");
}
}
}
The class Game is an inner class, by the way. Whenever a new piece goes down, the value of numDropped increments (as displayed in a JTextField) but then after a short while goes back to zero. Did I misplace the
numDropped = numDropped+1;
RightPanel.scoreTextField.setText(numDropped+"");
? Or because of something else like being static and stuff. Please help me. I’m kinda new in Java. Thank you very much!
The code you have posted will definitely never update the
scoreTextFieldto a value that is smaller than the value it used earlier.The first thing I would suspect is that there is some other code that updates
scoreTextFieldto a value that is not equal tonumDropped.Update
In your additionally posted code I see this:
This is wrong; delete it and uncomment the line above, that sets the text on the existing text field.
Moreover, you’ve got this code:
Clearly this will overwrite your
scoreTextFieldwith the game score, whereas in theGame‘s loop you are writing the number of dropped elements. Judging from the name of your variablescoreTextField, you should change the code inGameto update some other text field and not the one used to display score.Yet another thing that is wrong in your design is this:
You are starting another thread to control the gameplay. You must not update any Swing components outside the main GUI thread, the so-called Event Dispatch Thread (EDT). My suggestion is to redesign your code similar to what you have done above with your
timervariable: don’t start another thread; instead schedule anotherTimerwith the interval of 100 milliseconds, and implement the associatedactionPerformedbased on your current code inwhile (onPlay)— all you’ll need to change is that you don’t need the loop anymore. Of course,Thread.sleep(100)will also be redundant. When the game is over, justcancelyour timer task.Not directly related to the problem of this question, but this code
is also wrong: it doesn’t write to the instance variable
count, but just increments the supplied argument, immediately discarding it (it’s just a local variable).