[英]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.