2015-05-03 86 views
1

目前我有一个简单的射击游戏,其中精灵在屏幕上飞行,当他们被按下时,它会增加一个用户的分数。问题是我想拥有它,所以我已经为gameover声明了一个布尔值,当游戏开始时,它将被初始化为false,并在定时器耗尽时声明为true。我目前使用的代码是这样的,当定时器耗尽时gameover被设置为true,但由于某种原因,而不是默认为true,而不是等待定时器耗尽。任何想法为什么这是?游戏结束无法正常工作?

/* Member (state) fields */ 
private GameLoopThread gameLoopThread; 
private Paint paint; //Reference a paint object 
/** The drawable to use as the background of the animation canvas */ 
private Bitmap mBackgroundImage; 
private Sprite sprite; 
private int hitCount; 
/* For the countdown timer */ 
private long startTime ;   //Timer to count down from 
private final long interval = 1 * 1000;  //1 sec interval 
private CountDownTimer countDownTimer; //Reference to class 
private boolean timerRunning = false; 
private String displayTime;   //To display time on the screen 
private boolean gameOver; 
private int highscore = 0; 
/* Countdown Timer - private class */ 
private class MyCountDownTimer extends CountDownTimer { 

    public MyCountDownTimer(long startTime, long interval) { 
     super(startTime, interval); 
    } 
    public void onFinish() { 
     displayTime = "Times Over!"; 
     timerRunning = false; 
     countDownTimer.cancel(); 
     gameOver = true; 
     if (hitCount > highscore) { 
      highscore= hitCount; 
         } 

    } 

public void update(){ 
    //if (gameOver = true){ 
    sprite.update(); 
    //} 
} 

回答

0

我可能是错的,但这个gameOver = true这里出现这里评论,会创造这种行为。您正在使用分配(=)而不是等值验证(==)。

在这种情况下,您应该编写if (gameOver == true) {,或者简单地说:if (gameOver) {

除此之外,我不知道你的错误可能是什么,因为布尔基元变量的默认值确实是false

这是我做的一个小测试,以确定我的假设。我跑:

public class Test { 

    public static boolean x; 

    public static void main(String[] args) { 
     System.out.println(x); 
     if (x = true) 
      System.out.println(x); 
    } 

} 

而获得的输出:

false 
true 
+0

没错这个做到了,我会评论说出来,以确保游戏将运行相等验证工作。 – kendrick

相关问题