In my 2 player game, only one Healthbar takes damage, after the first player as well as the second player are being shot.
The code where the healthbar is being used in:
public void doBulletHit(int playerNum)
{
World myWorld = getWorld();
BattleWorld battleWorld = (BattleWorld)myWorld;
myPlayerNum = playerNum;
if ((playerNum == 1)&& isTouching(Bullet.class) && !(didShoot))
{
Bar bar = battleWorld.getBar();
Actor bull = (Actor) getOneIntersectingObject(Bullet.class);
bleed((Greenfoot.getRandomNumber(3)+2), bull.getX(), bull.getY());
getWorld().removeObject(bull);
bar.loseHealth();
if(bar.health <=0)
{
GameOver gameover = new GameOver(myPlayerNum);
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
if (++counter == 100) Greenfoot.setWorld(new TitleScreen());
}
}
else if ((playerNum == 0)&& isTouching(Bullet.class) && !(didShoot))
{
Bar bar2 = battleWorld.getBar();
Actor bull = (Actor) getOneIntersectingObject(Bullet.class);
bleed((Greenfoot.getRandomNumber(3)+2), bull.getX(), bull.getY());
getWorld().removeObject(bull);
bar2.loseHealth();
if(bar2.health <=0)
{
GameOver gameover = new GameOver(myPlayerNum);
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
if (++counter == 100) Greenfoot.setWorld(new TitleScreen());
}
}
}
