Hi. How can I access and change the variable "Lifes" in the class "Healthbar" from the class "Player"? I've already tried some solutions, but they did not work for me.


getWorld().getObjects(Healthbar.class).get(0).lives
public int getLives() { return lives; }
getWorld().getObjects(Healthbar.class).get(0).getLives()
int lives=getWorld().getObjects(Healthbar.class).get(0).lives;
public int lives;
public int lives=3;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { private Pfeil meinPfeil; int schussabstand=0; int lives=getWorld().getObjects(Healthbar.class).get(0).lives; /** * Act - do whatever the Spieler wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { getMouse(); bewegen(); schießen(); } public void bewegen() { turnTowards(MouseX, MouseY); if (Greenfoot.isKeyDown("w")) { move(4); } if (Greenfoot.isKeyDown("s")) { move(-4); } } public void schießen() { { { if(schussabstand > 0) { schussabstand -= 1; } if (Greenfoot.isKeyDown("Space") && (schussabstand == 0)) { getWorld().addObject(new Pfeil(getRotation()), getX(), getY()); schussabstand = 40; } } } int SpielerX=getX(); int SpielerY=getY(); } public static int MouseX = 0, MouseY = 0; private void getMouse() { MouseInfo mouse = Greenfoot.getMouseInfo(); if ( mouse != null ) { MouseX = mouse.getX(); MouseY = mouse.getY(); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Healthbar here. * * @author (your name) * @version (a version number or a date) */ public class Healthbar extends Actor { public int lives=3; public int getLives() { return lives; } /** * Act - do whatever the Leben wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Leben();// Add your action code here. } public void Leben() { if(lives==3) { setImage("Lebensanzeige3.png"); } if(lives==2) { setImage("Lebensanzeige2.png"); } if(lives==1) { setImage("Lebensanzeige1.png"); } if(lives==0) { setImage("Lebensanzeige0.png"); } } }
int lives=getWorld().getObjects(Healthbar.class).get(0).lives;
int lives=((HealthBar)(getWorld().getObjects(Healthbar.class).get(0)).lives;
int lives = ((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).lives;
((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).lives--;