简体   繁体   English

Java定时触发器

[英]Java Timing A Trigger

I want to trigger an action after a certain time, I've been googling how I would do this but I have had no luck I guess it's just the way my game is coded. 我想在一段时间后触发一个动作,我一直在谷歌搜索该怎么做,但是我没有运气,我想这只是我的游戏编码的方式。 Anyways I need to make it to where 30 minutes after the code a1 is triggered, the code a2 is triggered. 无论如何,我需要使其到达触发代码a1后30分钟的地方,触发代码a2。

a1: A1:

if (itemId == 608) {
        c.sendMessage("The scroll has brought you to the Revenants.");
        c.sendMessage("They are very ghastly, powerful, undead creatures.");
        c.sendMessage("If you defeat them, you may receive astounding treasures.");
        c.sendMessage("If you donate you may visit the Revenants anytime without a scroll.");
        c.getPA().movePlayer(3668, 3497, 0);
        c.gfx0(398);
        c.getItems().deleteItem(608, 1);
}

a2: a2:

c.getPA().movePlayer(x, y, 0);

There are plenty of ways to do timers in Java but to introduce yourself to a nice framework check out http://quartz-scheduler.org/ . 有很多方法可以使用Java执行计时器,但可以向自己介绍一个不错的框架,请访问http://quartz-scheduler.org/ Also Spring has quartz integration if you use it. 如果使用Spring,它也具有石英集成。

But more importantly if you are creating a game you will need a core technique of game programming called the event loop 但是更重要的是,如果您要创建游戏,则需要一种称为事件循环的游戏编程核心技术

This seems like a decent discussion of how to create a game architecture 这似乎是关于如何创建游戏架构的不错的讨论

You can use Thread.sleep(), but it will freeze your application if you call it in your main thread, so, create another thread and put your code in it. 您可以使用Thread.sleep(),但是如果您在主线程中调用应用程序,它将冻结您的应用程序,因此,请创建另一个线程并将代码放入其中。 Doing this you will not stop the main application. 这样做不会停止主应用程序。

Here is a simple example. 这是一个简单的例子。

public class MyThread implements Runnable {

    @Override
    public void run() {

        try {

            System.out.println( "executing first part..." );
            System.out.println( "Going to sleep ...zzzZZZ" );

            // will sleep for at least 5 seconds (5000 miliseconds)
            // 30 minutes are 1,800,000 miliseconds
            Thread.sleep( 5000L );

            System.out.println( "Waking up!" );
            System.out.println( "executing second part..." );

        } catch ( InterruptedException exc ) {
            exc.printStackTrace();
        }

    }

    public static void main( String[] args ) {
        new Thread( new MyThread() ).start();
    }

}

This will run just one time. 这将只运行一次。 To run several times, you will need an infinite loop that encloses the run method body (or a loop controled by a flag). 要运行几次,您将需要一个无限循环,该循环包围run方法的主体(或受标志控制的循环)。

You have some other options like: 您还有其他选择,例如:

Since this code uses Project Insanity , you should use the built-in scheduled event facility provided by server.event.EventManager . 由于此代码使用Project Insanity ,因此您应使用server.event.EventManager提供的内置计划事件功能。

Below is example code: 下面是示例代码:

if (itemId == 608) {
  c.sendMessage("The scroll has brought you to the Revenants.");
  c.sendMessage("They are very ghastly, powerful, undead creatures.");
  c.sendMessage("If you defeat them, you may receive astounding treasures.");
  c.sendMessage("If you donate you may visit the Revenants anytime without a scroll.");
  c.getPA().movePlayer(3668, 3497, 0);
  c.gfx0(398);
  c.getItems().deleteItem(608, 1);

  /* only if the parameter Client c isn't declared final */
  final Client client = c;
  /* set these to the location you'd like to teleport to */
  final int x = ...;
  final int y = ...;

  EventManager.getSingleton().addEvent(new Event() {

    public void execute(final EventContainer container) {
      client.getPA().movePlayer(x, y, 0);
    }
  }, 1800000); /* 30 min * 60 s/min * 1000 ms/s = 1800000 ms */
}

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

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