[英]Java Timing A Trigger
我想在一段时间后触发一个动作,我一直在谷歌搜索该怎么做,但是我没有运气,我想这只是我的游戏编码的方式。 无论如何,我需要使其到达触发代码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);
有很多方法可以使用Java执行计时器,但可以向自己介绍一个不错的框架,请访问http://quartz-scheduler.org/ 。 如果使用Spring,它也具有石英集成。
但是更重要的是,如果您要创建游戏,则需要一种称为事件循环的游戏编程核心技术
这似乎是关于如何创建游戏架构的不错的讨论
您可以使用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方法的主体(或受标志控制的循环)。
您还有其他选择,例如:
由于此代码使用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 */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.