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.


1 | getWorld().getObjects(Healthbar. class ).get( 0 ).lives |
1 2 3 4 | public int getLives() { return lives; } |
1 | getWorld().getObjects(Healthbar. class ).get( 0 ).getLives() |
1 | int lives=getWorld().getObjects(Healthbar. class ).get( 0 ).lives; |
1 | public int lives; |
1 | public int lives= 3 ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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" ); } } } |
1 | int lives=getWorld().getObjects(Healthbar. class ).get( 0 ).lives; |
1 | int lives=((HealthBar)(getWorld().getObjects(Healthbar. class ).get( 0 )).lives; |
1 | int lives = ((Healthbar)getWorld().getObjects(Healthbar. class ).get( 0 )).lives; |
1 | ((Healthbar)getWorld().getObjects(Healthbar. class ).get( 0 )).lives--; |