Tower Defence - Why is it my monster kills sometimes count as two kills?
I have a working tower defence game where I can place a tower (which is a special block identified with a airID) and that tower can kill monsters that navigate the path
Now everything works fine except for one issue. If I have multiple towers that are shooting at a monster at the same time the killed count can increase by one OR two (this seems to be random) also never more than two no matter how many towers are shooting.
Here is my code. I am stumped, any suggestions would be appreciated.
Code:
//Screen Class
public void run()
{
while(true)
{
// if default variables set and room in progress
if(!isFirst && healthCount > 0 && !roomWin)
{
// set up room physics and spawn monsters
room.physic();
monsterSpawner();
// set up monster physics (movement)
for( int i = 0 ; i < monsters.length ; i++ )
{
if( monsters[i].inGame)
{
monsters[i].physic();
}
}
}
else
{
// if the room has been completed set next room or complete game
if( roomWin )
{
if( winFrame >= winTime )
{
if( level == maxLevel )
{
System.exit(0);
}
else
{
level += 1;
define();
roomWin = false;
}
winFrame = 0;
}
else
{
winFrame += 1;
}
}
}
//Room Class
public void physic()
{
for( int y = 0 ; y < block.length ; y++ )
{
for( int x = 0 ; x < block[0].length ; x++)
{
block[y][x].physic();
}
}
if( Block.hasKilled )
{
screen.killed++;
System.out.println("killed " + screen.killed);
Block.hasKilled = false;
if( screen.roomCompletedCheck() )
{
screen.roomCompleted();
}
}
//Block Class
public void physic()
{
if(!shooting)
{
if( airID == Sprite.airTeslaTower){
for( int i = 0 ; i < screen.monsters.length ; i++ )
{
if( screen.monsters[i].inGame )
{
if( towerSquare.intersects(screen.monsters[i]) )
{
shooting = true;
shotMonster = i;
}
}
}
}
}
if (shooting)
{
if(damageFrame >= damageTime)
{
screen.monsters[shotMonster].loseHealth(1);
boolean dead = screen.monsters[shotMonster].checkDeath();
if(dead)
{
screen.monsters[shotMonster].deleteMonster();
int tempID = screen.monsters[shotMonster].monsterID;
screen.monsters[shotMonster].getMoney(tempID);
}
damageFrame = 0;
}
else
{
damageFrame++;
}
if( screen.monsters[shotMonster].checkDead() )
{
shooting = false;
shotMonster = -1;
hasKilled = true;
}
}
}
Any ideas what might be the problem?