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

2019/1/9

Need help with Health

Lewis12374 Lewis12374

2019/1/9

#
Here is the code for my health. It is not working, how do fix? Bullet Class
    public void hitPlayer() {
        Actor main = (Main) getOneObjectAtOffset(0, 0, Main.class);
        if (main != null) {
            main.health--;
            if(main.getHealth() == 0){
              //KillPlayer   
            }
            getWorld().removeObject(this);
            isInWorld = false;
        }
 
    }
Main/Player Class
    private int health = 2;
    
    public int getHealth(){
        return health;
    }
Ples Help
ErnieBoi ErnieBoi

2019/1/9

#
ʰᵉˡˡᵒ ˡᵉʷᶦˢ ᶦ ᵃᵐ ᵉʳⁿᶦᵉ ᵃⁿᵈ ᶦ ᵈᵒⁿᵗ ᵏⁿᵒʷ
Zamoht Zamoht

2019/1/9

#
Change "private int health = 2;" to "public int health = 2;" Also I believe you should move the die code to be within the if statement like this.
    public void hitPlayer() {
    Actor main = (Main) getOneObjectAtOffset(0, 0, Main.class);
    if (main != null) {
        main.health--;
        if(main.getHealth() == 0){
          //KillPlayer   
            getWorld().removeObject(this);
            isInWorld = false;
        }
    }
 
}
danpost danpost

2019/1/9

#
Lewis12374 wrote...
Here is the code for my health. It is not working, how do fix? << Codes Omitted >>
Change the beginning of line 2 in the Bullet class code above to "Main main = ...". The health field cannot be found otherwise.
You need to login to post a reply.