This site requires JavaScript, please enable it in your browser!
Greenfoot back
MisterUnknown
MisterUnknown wrote ...

2019/6/20

Only 1 Healthbar works in my 2 player game!

MisterUnknown MisterUnknown

2019/6/20

#
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());
            }  
        }
}
Super_Hippo Super_Hippo

2019/6/20

#
The code for both players is the same. Renaming the variable from "bar" to "bar2" doesn't change anything, the Bar which is returned from "getBar()" stays the same. You probably only need the code once and pass the playerNum to the getBar method, so the getBar method can then return the correct bar based on the playerNum.
MisterUnknown MisterUnknown

2019/6/20

#
Thanks for the advice ;) But can you give me like an example, since what you tell me sounds awfully familiar like something I have already tried once without any success.
Super_Hippo Super_Hippo

2019/6/20

#
Show your BattleWorld class, so I can see how you create your Bar objects and keep a reference to them. Basically, it should look like this:
Bar bar = battleWorld.getBar(playerNum);
public Bar getBar(int playerNum)
{
    //return the correct bar based on the playerNum
}
MisterUnknown MisterUnknown

2019/6/20

#
Here is the full code:
public class BattleWorld extends World
{
    Player p1 = new Player(1, "tank.png", "left", "right", "up", "down");
    Player p2 = new Player(2, "tank2.png", "a", "d", "w", "s");
    Bar bar = new Bar();
    Bar bar2 = new Bar();
    public static int p1X = 100;
    public static int p1Y = 300;
    public static int p2X = 700;
    public static int p2Y = 300;
    Rohr rohr1 = new Rohr(1, "Rohr1.PNG");
    Rohr rohr2 = new Rohr(2, "Rohr2.PNG");
    /**
     * Konstruktor für Objekte der Klasse BattleWorld
     * 
     */
    public BattleWorld()
    {    
        // Erstellt eine neue Welt mit 600x400 Zellen und einer Zell-Größe von 1x1 Pixeln.
        super(800, 600, 1);
        addObject(p1, 100, 300);
        addObject(p2, 700, 300);
        
        addObject(bar, 200, 40);
        addObject(bar2, 600, 40);
        addObject(rohr1,110, 300);
        addObject(rohr2, 710, 300);
        
        //addObject(playbutton, 300, 200);
    }
    public Bar getBar()
    {
        return bar;
       
        
    }
  
}
Super_Hippo Super_Hippo

2019/6/20

#
Yes, you always return "bar" and never "bar2". In your case, the method could look like this:
public Bar getBar(int playerNum)
{
    if (playerNum == 0)
    {
        return bar;
    }
    else
    {
        return bar2;
    }
}
Which is the same as
public Bar getBar(int playerNum)
{
    return playerNum==0 ? bar : bar2;
}
Or you can save the Bars in an array:
private Bar[] bars = {new Bar(), new Bar()};
Then you add the objects like this;
addObject(bars[0], 200, 40);
addObject(bars[1], 600, 40);
And the method will look like this:
public Bar getBar(int playerNum)
{
    return bars[playerNum];
}
Btw, it is not a good idea to have p1X, p1Y, p2X and p2Y as static ints in the world. Instead, have a getter method like the getBar method to return a player. Then you can get its location.
MisterUnknown MisterUnknown

2019/6/21

#
With this code, it continuous to take damage only from one healthbar, with the difference of the other healthbar being affected.
MisterUnknown MisterUnknown

2019/6/21

#
Actually It works now thanks to you Appreciated the big support :)
You need to login to post a reply.