我使用Slick 2D编程LWJGL中的2D游戏。我的问题是每个级别的倒计时仅在您第一次玩该级别时起作用。如果您在游戏中获胜或松开,我取消计时器,你回到主菜单后一段时间。但如果你现在想再次玩这个级别的游戏是疯狂的。感谢您的帮助。
EDIT1 :现在我想知道如何在使用相同的代码将第一个计时器从计时器中挂起后再创建。
这是我得到的错误消息:
Sat Sep 16 13:15:14 CEST 2017 ERROR:Timer already cancelled.
java.lang.IllegalStateException: Timer already cancelled.
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.scheduleAtFixedRate(Unknown Source)
at misc.Countdown.startTimer(Countdown.java:27)
at menu.LevelMenu.update(LevelMenu.java:58)
at org.newdawn.slick.state.StateBasedGame.update(StateBasedGame.java:266)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at main.Main.main(Main.java:15)
Sat Sep 16 13:15:14 CEST 2017 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:669)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at main.Main.main(Main.java:15)
这是我的倒计时文件:
package misc;
import java.util.Timer;
import java.util.TimerTask;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
public class Countdown {
public static int interval;
static Timer timer1;
private static volatile int runTimes = 0;
public static void startTimer(int secs) {
int delay = 1000;
int period = 1000;
timer1 = new Timer();
interval = secs;
//System.out.println(secs);
try{
runTimes++;
cancelTimerIfRunTwice();
timer1.scheduleAtFixedRate(new TimerTask() {
public void run() {
if(interval >= 1){
System.out.println(setInterval());
} else {
timer1.cancel();
timer1.purge();
runTimes = 0;
}
}
}, delay, period);
} catch(Error e){
System.out.println(e);
}
}
public static void createTimer(Graphics g) {
if(interval > 0){
g.setColor(Color.white);
g.drawString("Rest Time: " + Integer.toString(interval), 830, 10);
} else {
g.setColor(Color.white);
g.drawString("Rest Time: 0", 830, 10);
}
}
private static final int setInterval() {
if (interval <= 1){
try{
timer1.cancel();
timer1.purge();
} catch(Error e){
System.out.println(e);
}
}
return --interval;
}
public static void cancelTimer() {
try{
timer1.cancel();
timer1.purge();
} catch(Error e){
System.out.println(e);
}
}
private static void cancelTimerIfRunTwice() {
if(runTimes >= 2) {
try{
timer1.cancel();
timer1.purge();
} catch(Error e){
System.out.println(e);
}
}
}
}
这里所有引用其他类的代码
从级别菜单:
//Level1 button
if((posX > 425 && posX < 490) && (posY > 410 && posY < 430)){
//System.out.println("drauf");
if(Mouse.isButtonDown(0)){
CreateMusicAndSound.soundMenuClick();
Player.interation = 5;
CreateMusicAndSound.bgmusic.stop();
sbg.enterState(2);
CreateMusicAndSound.musicFirstLevel();
//create time for the level
try{
Countdown.startTimer(30);
} catch(Error e){
System.out.println(e);
}
}
}
从胜利的等级或从等级松散:
public static void Win(Graphics g, StateBasedGame sbg){
g.setColor(Color.black);
g.drawString("You Win!", 400, 200);
if(Player.GameOverSound){
CreateMusicAndSound.soundYouWin();
Player.GameOverSound = false;
}
Player.interation = 0;
CreateMusicAndSound.bgmusicLevel.stop();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
sbg.enterState(0);
//Setze den Player wieder auf anfangs posi
Player.HitboxPlayer.setX(30);
Player.HitboxPlayer.setY(420);
//cancel den coundown timer
try{
Countdown.cancelTimer();
} catch(Error e){
System.out.println(e);
}
//Setze Points auf 0
Player.points = 0;
}
}, 2000);
}
public static void Loose(Graphics g, StateBasedGame sbg){
g.setColor(Color.black);
g.drawString("Game Over!", 400, 200);
if(Player.GameOverSound){
CreateMusicAndSound.soundGameOver();
Player.GameOverSound = false;
Player.HitboxPlayer.setX(30);
Player.HitboxPlayer.setY(420);
}
Player.interation = 0;
CreateMusicAndSound.bgmusicLevel.stop();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
sbg.enterState(0);
//Setze den Player wieder auf anfangs posi
Player.HitboxPlayer.setX(30);
Player.HitboxPlayer.setY(420);
//cancel den coundown timer
try{
Countdown.cancelTimer();
} catch(Error e){
System.out.println(e);
}
//Setze Points auf 0
Player.points = 0;
}
}, 2000);
}
}
如果你需要更多的信息,只需写一条评论我试着尽快回答它。 PS:如果某些句子或单词是疯狂的,那么我仍然是学生如此原谅。 :d