In my game, I am using a boolean variable called
onScreen
and it is set to true everytime the game starts. I also have an int variable called
onScreenTime
which is set to 180 when game starts. These are used to portray the text "Prepare for Wave X" on the screen for a set amount of time. Since onScreenTime is 180, and the fps is 60, the text is only displayed for 2 seconds. After 2 seconds have passed, onScreen is set to false, and the game continues. The code:
if (onScreen){
g.setFont(new Font("Geneva", Font.BOLD, 50));
g.setColor(Color.BLACK);
g.drawString("Prepare for wave: " + currentLevel,400,500);
}
draws the text on the screen only when onScreen = true. The following code is in the update() method and manipulates the two variables mentioned so far:
onScreenTime--;
if (onScreenTime == 0){
onScreen = false;
}
Basically, onScreenTime is decreased by 1 every time update() is called (60 times a second) and if it is equal to 0, then onScreen = false. Then in the same update() method, I have two if statements which should be pretty self-explanatory:
if (score > 500){
currentLevel = 2;
onScreen = true;
onScreenTime = 180;
}
if (score > 1000){
currentLevel = 3;
onScreen = true;
onScreenTime = 180;
}
And finally, in the same update() method, I have
if (currentLevel == 1 && onScreen == false){
wave(2,enemyP,100);
}
if (currentLevel == 2 && onScreen == false){
wave(3,enemyP1,90);
}
if (currentLevel == 3 && onScreen == false){
wave(3,enemyP2,80);
}
currentLevel decides the enemy, their drawn image, how many are drawn on a screen at a time, and how often they spawn. wave() is a method that creates the waves for each level. It is represented by:
public void wave(int amount, Image i, int seconds){
for (int x = amount; x > 0; x--){
if (frameCount % seconds == 0){
Random r = new Random();
int randX = r.nextInt(width - 20)+10;
army.add(new EnemyShip(randX,(int)(Math.random()*100)-300,enemyH,enemyW,i,10));
}
}
}
i is image, enemyH and enemyW = 30, and the 10 is life. That will be changed later. My issue is that once the text "Prepare for wave: 2" shows up, it does not disappear. The first "Prepare for wave: 1" works and disappears fine, but the second does not, thus no enemies will be spawned.
EDIT: As pointed out in the comments, I wrote 2 seconds, and it should be for 3. EDIT: I changed my previous method to this:
if (onScreenTime <= 0){
onScreen = false;
onScreenTime = 180;
}
and also changed previous method to this:
if (score == 500){
currentLevel = 2;
onScreen = true;
onScreenTime = 180;
}
if (score == 1000){
currentLevel = 3;
onScreen = true;
onScreenTime = 180;
}
The score goes up by 20 for every hit. I believe the problem was that I had it set to if(score > 500) instead of == 500.
Try changing
onScreenTime--;
if (onScreenTime == 0){
onScreen = false;
}
to
onScreenTime--;
if (onScreenTime <= 0){
onScreen = false;
onScreenTime = 180;
}
A race condition can cause onScreenTime getting below zero, causing hard to track issues.
resetting onScreenTime back to 180 will (most likely) make your current problem disappear.
BTW:
Since onScreenTime is 180, and the fps is 60, the text is only displayed for 2 seconds.
I expect it to be 3 seconds: 180/60=?