Java - 由计时器控制的多个对象(用于游戏)

发布于 2024-12-11 01:55:46 字数 956 浏览 0 评论 0原文

我目前正在开发一款小游戏,其中包含一些在不同时间段内随机弹出的目标。由于目标是物理目标,因此实际游戏将从电路板获取 I/O。

我的问题是,目前我有一个 java.util.Timer 每 2 秒触发一次。一旦触发,就会显示一个随机目标(到目前为止效果很好)。问题是我想在计时器仍在运行并触发其他目标的同时显示 1-5 秒之间的随机目标。

我没有收到任何错误,目标显示但从未消失。我猜这是某种线程问题,也许因为我正在使用 this.* ,目标对象只是以某种方式迷失在下界中!在搜索这里的问题后,我想出了这个:

public class Target implements Runnable(){

  ...

    public void displayFor(int seconds){
        this.display();

        Executors.newSingleThreadScheduledExecutor().schedule(this,time,
                TimeUnit.SECONDS);

        this.setDisplayed(false);
    }

    @Override
    public void run() {
        this.destroy();
    }

}

基本上,初始游戏计时器(设置目标显示)调用 displayFor(2) 方法,该方法在时间过去后运行目标 run 方法。但目标仍然不会消失。

我尝试了多种不同的方法来做到这一点,例如 displayFor() 设置另一个 java.util.Timer,并且我还尝试使用 Quartz 库(说实话,这似乎有点矫枉过正)无论如何)但仍然无法让它工作。由于没有错误消息,我真的被这个问题困扰了。

我没有包含很多代码,因为我认为它没有那么相关,但如果你们需要更多信息来帮助,请告诉我:)

I'm currently working on a small game that consisted of a few targets that pop up at random for various amounts of time. The actual game will get it's I/O from a circuit board since the targets are physical.

My problem is that currently I have a java.util.Timer that fire's off every 2 seconds. Once it is triggered a random target will be displayed (which works fine so far). The problem is that I want to display the targets for a random number of seconds between 1-5 whilst the timer is still running and setting off other targets.

I get no errors and the targets display but never disappear. I guess it's some sort of Thread issue and that maybe since I'm using this.* the Target objects are just somehow getting lost in the nether! After searching around the questions here I have come up with this:

public class Target implements Runnable(){

  ...

    public void displayFor(int seconds){
        this.display();

        Executors.newSingleThreadScheduledExecutor().schedule(this,time,
                TimeUnit.SECONDS);

        this.setDisplayed(false);
    }

    @Override
    public void run() {
        this.destroy();
    }

}

Basically the initial game timer (that sets of the Targets display) calls the displayFor(2) method which runs the targets run method after the time passed. The Targets still won't disappear though.

I have tried a number of different ways of doing this like the displayFor() setting off another java.util.Timer and I also had a go at using the Quartz library (which to be honest seemed like overkill anyway) and still can't get it to work. Since there are no error messages I'm really stuck with this one.

I've haven't included a lot of the code because I don't think it's that relevant but if you guys need more information to help just let me know :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-12-18 01:55:46

我设法让它工作。对于处于类似情况的任何人来说,这是正确的代码。

public class Target{

private Timer timer;

   ...

   public void displayFor(int seconds) {
     // send the output
     BoardInterface.SetDigitalChannel(this.getId());

     // calculate the delay
     long time = seconds * 1000;

     // create a new timer and schedule the new task
     timer = new Timer();
     timer.schedule(new TargetTimer(this), time);

     this.setDisplayed(true);
  }
}

class TargetTimer extends TimerTask{
   Target target;

   public TargetTimer(Target t){
       this.target = t;
   }

   @Override
   public void run() {
       target.destroy();
   }

}

不确定这是否是一个好方法,但它确实有效。如果您发现任何可以改进的地方,请告诉我。谢谢你们!

I managed to get it working. Here's the correct code for anyone in a similar situation.

public class Target{

private Timer timer;

   ...

   public void displayFor(int seconds) {
     // send the output
     BoardInterface.SetDigitalChannel(this.getId());

     // calculate the delay
     long time = seconds * 1000;

     // create a new timer and schedule the new task
     timer = new Timer();
     timer.schedule(new TargetTimer(this), time);

     this.setDisplayed(true);
  }
}

class TargetTimer extends TimerTask{
   Target target;

   public TargetTimer(Target t){
       this.target = t;
   }

   @Override
   public void run() {
       target.destroy();
   }

}

Not sure if this is a good way of doing it but it works. If you notice anything that could be improved please let me know. Thanks guys!

稀香 2024-12-18 01:55:46

也许您应该告诉我们 display 方法的作用。

您是否取消destroy/析构函数代码中显示目标?

我建议而不是 void display()

public void setDisplayed(boolean display){
  if(display) {
    ///... do appropriate circuit output to turn on target
  } else {
    /// ... do appropriate circuit output to turn off target
  }
}

当然

public void run(){
  setDisplayed(false);
  destroy();
}

Perhaps you should tell us what the display method does.

Are you un-displaying the target in the destroy/destructor code?

I'd recommend, instead of void display():

public void setDisplayed(boolean display){
  if(display) {
    ///... do appropriate circuit output to turn on target
  } else {
    /// ... do appropriate circuit output to turn off target
  }
}

and of course

public void run(){
  setDisplayed(false);
  destroy();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文