将两个类链接在一起 - Java

时间:2013-12-06 09:49:15

标签: java

下面的课程找不到计时器" time = new Timer",我看不出原因?我在这里错过了什么。谢谢你的帮助。我曾经让它工作了一段时间,但不知怎的,我必须改变一些东西,不能让它再次运作

    public ZombieDefense(GameScreen pointer) {
    setSize (1024,768);
    framePointer = pointer;
    time = new Timer();
    timer.start();
}


    @Override
    public void paint(Graphics g) {
        g.drawImage(background.getImage(), 0, 0, 1024, 768, null);
        survivor.draw(g);
        barricade.draw(g);
        int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
        int fontSize = (int)Math.round(22.0 * screenRes / 72.0);
        Font font = new Font("Arial", Font.PLAIN, fontSize);
        g.setFont(font);
        g.setColor(Color.white);
        g.drawString("WAVE " + wave, 450, 30);
        g.drawString("Base Health: " + barricade.health, 250, 700);
        g.drawString("Enemies remaining:  " + totalzomb, 650, 700);
        String s = "Time: " + time.counter;
        g.drawString(s,260,30);
        g.setColor(Color.red);
        g.fillRect (420,670,200,40);
        g.setColor(Color.green);
        g.fillRect (420,670,barricade.health,40);

这是保存Timer和计数器的类

public class Stopwatch implements Runnable {

    int counter;
    Thread Timer = new Thread(this);

    public Stopwatch()
    {
    Timer.start();
    }

    @Override
    public void run() {
  counter = 0;

1 个答案:

答案 0 :(得分:3)

您的问题

timer.start();

应该是

time.start();

为什么?

看看你的宣言:

time = new Timer();

这说明如下。

// Create a Timer object and return the reference to that object.
new Timer();

// Store that reference and call it "time"
time = new Timer();

从那时起,您可以通过参考访问该对象,并且您已将该引用命名为time,而不是timer

只是一个想法

您正在创建一个名为Stopwatch的单独类,但您从未使用它?我想,你的意思是:

time = new Stopwatch();
// the timer is started in the StopWatch constructor, so we don't need to do it.