简体   繁体   English

计时器启动/停止参数

[英]Timer start/stop parameters

I've made leaps and bounds in skill and progress since joining this community. 自加入该社区以来,我在技能和进步方面取得了长足发展。 You all are a huge help. 你们都有很大的帮助。 I'm having trouble with giving a timer that I've implemented certain parameters for when it starts and stops. 我无法提供一个计时器,该计时器在启动和停止时已经实现了某些参数。

I either get errors saying "the local variable timer may not have been initialized" or I get no errors, but nothing happens. 我或者收到说“本地变量计时器可能尚未初始化”的错误消息,或者没有收到任何错误消息,但是什么也没发生。 Maybe I have the timer in the wrong place? 也许我把计时器放在错误的地方了?

If I put timer.start(); 如果我把timer.start(); in the constructor too everything works fine, but then the timer has to start when the program is initialized. 在构造函数中,一切都可以正常工作,但是随后必须在初始化程序时启动计时器。 I would really like to have the timer not start until a certain parameter is met. 我真的很想让计时器在满足某个参数之前不启动。 Say, for instance, until the int p1Laps=1; 举例来说,直到int p1Laps=1; but if I place timer.start(); 但是如果我放置timer.start(); into an if-statement in the constructor (ie if(p1Laps>=1) { timer.start(); } the timer doesn't ever start. 进入构造函数中的if语句(即if(p1Laps>=1) { timer.start(); } ,计时器永远不会启动。

I've tried placing timer.start(); 我试过放置timer.start(); in various places and have either gotten no response or generated an error about the lack of local variable timer . 在不同的地方都没有反应或产生关于缺少局部变量timer的错误。

A second, somewhat related problem I have is the inability to put any parameters in place to call on timer.stop(); 我遇到的第二个相关问题是无法放置任何参数以调用timer.stop(); without getting the aforementioned "local variable timer may not have been initialized" error. 而没有得到上述的“局部变量计时器可能尚未初始化”错误。 I've left timer.stop(); 我离开了timer.stop(); where I think it needs to be in the code, but it receives that error. 我认为它需要在代码中,但它收到该错误。

So in short, I want to be able to tell the timer to start when a parameter is met, namely when a player has completed a lap. 简而言之,我希望能够告诉计时器在满足参数时启动,即当玩家完成一圈时。 And I want to be able to tell the timer to stop when it reaches a value. 而且我希望能够告诉计时器在达到某个值时停止计时。

Thanks in advance for the great advice I'm sure I'll receive. 在此先感谢您提供的宝贵建议,我相信我会收到的。 Note: this is not the whole code, just relevant information. 注意:这不是全部代码,而只是相关信息。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;

public class RacerDoom extends JFrame {
    int counter = 0;
    int p1Laps = 0;
public RacerDoom() {
        //create JFrame
        super("Racer Doom Squared");
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        //set up Timer
        final Timer timer=new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(counter>=10) {
                    timer.stop(); //error here reads "local variable timer may
                                    //not have been initialized"
                }
                else{
                   counter++;
                   }
                System.out.println(counter);
            }
        });
        //inner class threads
        Move1 m1 = new Move1();
        m1.start();
        //start timer
        if(p1Laps>=1) {
            timer.start(); //error here is that timer will not start when
                            //p1Laps>=1
        }
    }
    private class Move1 extends Thread implements KeyListener {
        public void run() {
            addKeyListener(this);
            while(true) {
                try {
                    repaint();
                    //collisions
                    if(p1.intersects(finishtop)&&p1Direction==UP&&p1cross!=true){
                        p1cross=true;
                        p1Laps++;
                        p1Boost++;
                        counter=0;
                        System.out.println(p1Laps);
                    }
                    if(p1.intersects(finishtop)==false) {
                        p1cross=false;
                    }
    public static void main (String [] args) {

        new RacerDoom();
    }
}

As you want to start and stop the timer at different places in the code you should make it member variable. 当您要在代码的不同位置启动和停止计时器时,应将其设置为成员变量。 This will fix the problem where you are trying to stop the timer inside the action listener. 这将解决您试图停止动作监听器中的计时器的问题。

The variable p1Laps will not change in the constructor (after you have initialized it to 0) so you need to start the timer where you change the value of plLaps. 变量p1Laps在构造函数中不会更改(将其初始化为0后),因此需要在更改plLaps值的地方启动计时器。 I am not sure if it is safe to call timer.start() from another thread (Move1). 我不确定从另一个线程(Move1)调用timer.start()是否安全。 So it may be safer to start timer with SwingUtilities.invokeLater(). 因此,使用SwingUtilities.invokeLater()启动计时器可能更安全。

Quick fix: 快速解决:

Rather than 而不是

timer.stop();

Do

((Timer)e.getSource()).stop();

The ActionEvent's getSource method will return a reference to the object that calls the actioPerformed method (the Timer), so this should work. ActionEvent的getSource方法将返回对调用actioPerformed方法(计时器)的对象的引用,因此该方法可以正常工作。

There may be other issues with your code including your background thread without a Thread.sleep(...), your use of KeyListeners rather than Key Binding, your adding a KeyListener in a background thread,... 您的代码可能还有其他问题,包括没有Thread.sleep(...)的后台线程,使用KeyListeners而不是Key Binding,在后台线程中添加KeyListener等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM