我想在一段时间后触发一个动作,我一直在谷歌搜索我会怎么做但我没有运气我想这就是我的游戏编码方式。 无论如何,我需要在触发代码a1后30分钟将其发送到触发代码a2的位置。
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:
c.getPA().movePlayer(x, y, 0);
答案 0 :(得分:2)
有许多方法可以在Java中使用计时器,但是要向自己介绍一个很好的框架,请查看http://quartz-scheduler.org/。如果你使用它,Spring也有石英集成。
但更重要的是,如果您正在创建游戏,则需要一种称为event loop的游戏编程核心技术
的体面讨论答案 1 :(得分:1)
您可以使用Thread.sleep(),但如果您在主线程中调用它,它将冻结您的应用程序,因此,创建另一个线程并将您的代码放入其中。这样做不会停止主应用程序。
这是一个简单的例子。
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();
}
}
这只会运行一次。要运行几次,您将需要一个包含run方法体的无限循环(或由标志控制的循环)。
您还有其他一些选择:
答案 2 :(得分:1)
由于此代码使用Project Insanity,因此您应使用server.event.EventManager
提供的内置预定事件工具。
以下是示例代码:
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 */
}