目前我有一个简单的射击游戏,精灵在屏幕上飞过,当他们被按下时,它会将用户分数增加一。问题是我想拥有它所以我已经为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 :(得分: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