0

我这里有我的俄罗斯方块项目的代码片段。

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+"");
        }
    }
}

顺便说一下,这个类Game是一个内部类。每当一个新的部分下降时,numDropped增量的值(如 JTextField 中显示的那样)但不久之后又回到零。我放错地方了吗

numDropped = numDropped+1;
RightPanel.scoreTextField.setText(numDropped+"");

? 或者因为其他的东西,比如存在之类的static东西。请帮我。我是Java的新手。非常感谢你!

4

2 回答 2

2

您发布的代码绝对不会将 更新scoreTextField为小于之前使用的值的值。

我怀疑的第一件事是有一些其他代码更新scoreTextField为不等于numDropped.

更新

在您另外发布的代码中,我看到了这一点:

RightPanel.scoreTextField = new JTextField(numDropped+"");

这是错误的;删除它并取消注释上面的行,该行设置现有文本字段上的文本。

此外,您还有以下代码:

timer = new Timer(
                500, new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        scoreTextField.setText("" + game.getScore());
                        int scoreForLevelUpdate = game.getScoreForLevelUpdate();
                        if (scoreForLevelUpdate >= Tetris.PER_LEVEL_SCORE && scoreForLevelUpdate > 0)
                            game.levelUpdate();
                    }
                }
        );

        timer.start();

显然,这将scoreTextField用游戏分数覆盖您,而在Game' 循环中,您正在写入丢弃元素的数量。从变量的名称来看scoreTextField,您应该更改代码Game以更新其他一些文本字段,而不是用于显示分数的文本字段。

您的设计中的另一件事是错误的:

Thread thread = new Thread(new Game());
thread.start();

您正在启动另一个线程来控制游戏玩法。您不得在主 GUI 线程(即所谓的事件调度线程 (EDT))之外更新任何 Swing 组件。我的建议是重新设计您的代码,类似于您在上面对timer变量所做的操作:不要启动另一个线程;而是以 100 毫秒的间隔安排另一个,并根据您当前的代码Timer实现关联——您需要更改的只是不再需要循环。当然,也会是多余的。当游戏结束时,只是你的计时器任务。actionPerformedwhile (onPlay)Thread.sleep(100)cancel

与this question的问题没有直接关系,但是这段代码

public void setCount(int count){
    count++;
}

也是错误的:它不写入实例变量count,而只是增加提供的参数,立即丢弃它(它只是一个局部变量)。

于 2012-10-10T13:34:16.307 回答
1

我可以看到它如何设置为零的唯一两个地方如下:

public int getScore() {
        if (board != null) 
            return board.getScore();
        return 0;
    }

  public int getScoreForLevelUpdate() {
        if (board != null) 
            return board.getScoreForLevelUpdate();
        return 0;
    }

在某些时候,您的board对象会被清空,可能是在重置功能中。将调试器绑定到进程并在这两个函数中放置一个断点。

于 2012-10-10T13:43:52.783 回答