我的遊戲幾乎完成,除了此錯誤限制進一步進行。我是新手程序員。我有以下代碼我不知道爲什麼下一條語句在前一條之前執行?
類結束
public class End
{
public void render(Graphics g)
{
Font font = new Font("TimesNewRoman", Font.BOLD, 15);
Font font2 = new Font("TimesNewRoman", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.CYAN);
g.setFont(font2);
g.setColor(Color.WHITE);
tex.setDead();
g.drawString("Died !", 590, 240);
g.drawImage(tex.player, 590, 250, game);
g.drawString("Survival Score: " + game.count, 530, 360);
if(game.count > FileManager.getHighScore())
{
g.setColor(Color.YELLOW);
g.drawString("Your Survival will not be forgotten !", 480, 400);
}
g.drawString("Press 'Enter' for retry...", 500, 600);
g.drawString("Press 'Space' to go Main Menu...", 460, 640);
FileManager.setHighScore(game.count);
game.resetStates();
}
}
game.resetStates顯示當前得分計數之前正在執行(其復位得分計)。雖然其他陳述工作正常。我想要做的是顯示分數(不高分,它工作正常),但它顯示1而不是當前分數。注意:方法resetStates()將其重置爲1.但是我想在顯示分數後執行此操作。
主要的遊戲類,其中game.resetStates可以發現低於
public class Game extends Canvas implements Runnable
{
public static int count = 1;
private End end;
public static enum STATE{
MENU,
SCORE,
START,
GAME,
PAUSE,
END
};
public static STATE State = STATE.MENU;
public void init()
{
requestFocus();
end = new End(this, tex);
c.createEnemy(count);
score = new Font("TimesNewRoman", Font.BOLD, 15);
}
private synchronized void start()
{
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop()
{
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
public void run()
{
//That Frame Rate stuff.
}
private void tick()
{
if(State == STATE.GAME)
{
player.tick();
c.tick();
}
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//================================
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
if(State == STATE.GAME)
{
g.drawImage(map, 0, 0, getWidth(), getHeight(), this);
player.render(g);
c.render(g);
g.drawImage(mapVision, 0, 0, getWidth(), getHeight(), this);
g.setFont(score);
g.setColor(Color.WHITE);
g.drawString("Survival: " + count, 620, 30);
g.drawString("Press 'Enter' to Pause...", 10, 710);
}
else if(State == STATE.MENU)
{
menu.render(g);
}
else if(State == STATE.SCORE)
{
hol.render(g);
}
else if(State == STATE.START)
{
start.render(g);
}
else if(State == STATE.PAUSE)
{
pause.render(g);
}
else if(State == STATE.END)
{
end.render(g);
}
//================================
g.dispose();
bs.show();
}
public void resetStates()
{
count = 1;
player.setX(640);
player.setY(360);
c.removeAllEntities();
c.createEnemy(1);
tex.setDown();
}
}
最後,當玩家相交敵人包圍盒,它的類是跟隨,階級敵人這個resetState()發生
public void tick()
{
y+= speed;
if(y >= (Game.HEIGHT * Game.SCALE))
{
speed = r.nextInt(3) + 2;
y = 0;
x = r.nextInt(Game.WIDTH * Game.SCALE);
game.count ++;
game.enemyArmy();
}
if(this.getBody().intersects(game.player.getBody()))
{
game.State = STATE.END;
}
}
public void render(Graphics g)
{
g.drawImage(tex.spike, x, y, null);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBody()
{
return new Rectangle(this.x, this.y + 10, this.tex.spike.getWidth(null), this.tex.spike.getHeight(null));
}
}
如果我的代碼不好,對不起,初學者在這裏:P
請向我們展示與問題相關的代碼。不要在這裏轉儲你的整個代碼。它不會以任何方式幫助你 – TheLostMind
可能發生的是你的render()函數正在執行多次。所以第一幀它實際上打印你期待的計數,但在下一幀計數已經等於1 – KuramaYoko
非常感謝,得到它,解決了它!像企鵝一樣工作! – Xirexor