Reputation: 3661
When My character dies I want all of the enemies to be removed when I click "restart" on the game-over screen. But there is always 1-2 enemies left on the field when it restarts, I really dont understand.
This is my reset code:
public void resetStats() {
for (int i = 0; i < enemies.size(); i++) {
enemies.remove(i);
}
System.out.print("ENEMIES: " + enemies.size());
enemyCounter = 0;
nbrOfEnemiesKilled = 0;
spawnEnemyTimer = 0;
score.setScore(0);
bulletsFired = 0;
nbrOfBullets = 100;
}
This method is called when I press the "restart" button on my game-over screen. Everything gets reset properly except the enemies that are left on screen.
Im using game states and the game is only updated during gameState=GAMEPLAY
, and I set gameState=GAMEOVERSCREEN
when my character dies, this brings up the screen. My thred is still going in the background while all of this happens since Im not running my game-over screen on a UI-thread, so the remaining characters will jump back and forth a few pixels while in this state.
I dont know how tu run my screens on an UI-thread since Im calling it from a View Class
during real-time.
I've tried deleting the enemies before it goes in to a new gamestate, but that does not fix the problem either. Im lost. Should I run my screens on an UI-thread and paus my game-loop while in this state? How would I go about doing this from a view-class since I need Context
to run on Ui-thread.
If any further explenation is required, please let me know.
Upvotes: 0
Views: 93
Reputation: 587
The termination value of the for loop being 'enimies.size()' will be problematic since this is evaluated every time the loop is run through. Since the loop makes the size smaller, the termination value decreases by one every time i increases by one, essentially making the loop terminate twice as fast as it would if the 'enimies.size()' value wasn't changing. Therfore, it's best to use non-changing values as for-loop termination values. The other answers supply sufficient enough information to fix the error, but this is just an extra tip for the future.
Upvotes: 0
Reputation: 361
The variable i
is always incremented while enemies.size()
is always decremented (by removing it):
for (int i = 0; i < enemies.size(); i++) {
enemies.remove(i);
}
So if you have 5 enemies [0, 1, 2, 3, 4] it'll turn out this way:
[0, 1, 2, 3, 4]
remove(0)
[1, 2, 3, 4]
remove(1)
[1, 3, 4]
remove(2)
[1, 3]
And that's it because i = 3
isn't smaller than enemies.size() = 2
anymore!
EDIT: As stated above enemies.clear()
should do the trick or use
while (enemies.size() > 0)
{
enemies.remove(0);
}
in case you need it.
Upvotes: 0
Reputation: 11937
I think this is your problem:
for (int i = 0; i < enemies.size(); i++) {
enemies.remove(i);
}
Suppose you have 3 enemies left. The loop starts at 0 and removes the first enemy. Now you have 2 enemies left at indexes 0 and 1. The value of i
is now 1 so you're able to remove 1 more enemy. Now you have 1 enemy left at index 0 but the value of i
is 2, so there would still be 1 enemy left in the Collection
.
Try replacing enemies.remove(i)
with enemies.remove(0)
. Or, assuming enemies
is a Collection
, why don't you just do enemies.clear();
?
Upvotes: 2