I realize this issue has come up a thousand times, but please bear with me ;). I've read through a bunch of posts about this issue, but most people seem to solve it by adding a "lives" variable in the Actor. Unfortunately, we've set up and developed a lengthy scenario already and we've placed the lives in MyWorld, because otherwise we somehow coudn't get it to display correctly and transfer correctly to the next level. But now, of course, we're having problems accessing the private lives variable in MyWorld from the Actor class Mario. I'll include the code below and would be grateful for any help. Thank you in advance!
MyWorld:
Mario:
public class MyWorld extends World { protected int score; protected int zahl; /** * Konstruktor für Objekte der Klasse MyWorld * */ /** * Konstruktor für Objekte der Klasse MyWorld * */ public MyWorld() { // Erstellt eine neue Welt mit 1300x700 Zellen und einer Zell-Größe von 1x1 Pixeln. super(1300, 700, 1); showText("Punktestand: " + score, 100,100); getPunktestand(); zahl = 3; showText ("Leben: " + zahl, 100,80); getLeben(); Mario mario = new Mario(); //Setzt Mario addObject(mario,55,310); setPaintOrder(Mario.class,Häschen.class,MyWorld.class); //Legt den Layeraufbau fest } public void erhoeheScore(int anzahl) { score = score + anzahl; showText("Punktestand: " + score, 100,100); } public int getPunktestand (){ //Getter-Methode zur Ausgabe der Punkte return this.score; } public void erhoeheLeben(int z) { zahl = zahl + z; showText ("Leben: " + zahl, 100,80); } public int getLeben() { return this.zahl;
public class Mario extends Actor { private static final int acceleration = 2; // down (gravity) private static final int speed = 5; // running speed (sideways) private static final int jumpStrength = 28; // jump strength(Sprungkraft) private int vSpeed = 0; // current vertical speed private GreenfootImage NicoleMarioLaufenL80 = new GreenfootImage("NicoleMarioLaufenL80.png"); private GreenfootImage NicoleMarioLaufenL = new GreenfootImage("NicoleMarioLaufenL.png"); private GreenfootImage NicoleMarioLaufenR80 = new GreenfootImage("NicoleMarioLaufenR80.png"); private GreenfootImage NicoleMarioLaufenR = new GreenfootImage("NicoleMarioLaufenR.png"); private GreenfootImage NicoleMarioJumpR = new GreenfootImage("NicoleMarioJumpR.png"); private GreenfootImage NicoleRakete = new GreenfootImage("NicoleMarioRakete.png"); public void act() { //this.get(World().showText(1200,100); checkKeys(); checkFall(); if(isTouching(Tor.class)) //Hier wird bei Berührung der Wolke(Tor) das nächste Level geöffnet {Greenfoot.setWorld(new Level2(getWorldOfType(MyWorld.class).getPunktestand())); } if(isTouching(Tor2.class)) //Hier wird bei Berührung der Wolke(Tor) das nächste Level geöffnet { Greenfoot.setWorld(new Level3(getWorldOfType(MyWorld.class).getPunktestand())); } if(isTouching(Iglu.class)) //Hier wird bei Berührung der Wolke(Tor) das nächste Level geöffnet {Greenfoot.setWorld(new Level4(getWorldOfType(MyWorld.class).getPunktestand())); } if(isTouching(Level5.class)) { setImage(NicoleRakete); } ende(); end(); // Greenfoot.playSound("mario.mp3"); } public void ende() //GameOver { if (getY()+5 >= getWorld().getHeight()) { GameOver gameOver = new GameOver(); getWorld().addObject(gameOver, getWorld().getWidth()/2, getWorld().getHeight()/2); getWorld().removeObject(this); gameEnd(); } } private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { //setImage("NicoleMarioLaufenL80.png"); moveLeft(); if(getImage()== NicoleMarioLaufenL80) { setImage(NicoleMarioLaufenL); } else { setImage(NicoleMarioLaufenL80); } } if (Greenfoot.isKeyDown("right") ) { //setImage("NicoleMarioLaufenR80.png"); moveRight(); if(getImage()== NicoleMarioLaufenR80) { setImage(NicoleMarioLaufenR); } else { setImage(NicoleMarioLaufenR80); } } if (Greenfoot.isKeyDown("space") ) { if (onGround()) { setImage(NicoleMarioJumpR); jump(); } } } private void jump() { setVSpeed(-jumpStrength); fall(); } private void checkFall() { if (onGround()) { setVSpeed(0); } else { fall(); } } public void moveRight() { setLocation ( getX() + speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); } public boolean onGround() { Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 +vSpeed, null); return under != null; } public void setVSpeed(int speed) { vSpeed = speed; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; //if ( atBottom() ) // gameEnd(); } private boolean atBottom() { return getY() >= getWorld().getHeight() - 2; } public void end() //GameOver { if (isTouching(Gegner.class)) { GameOver gameOver = new GameOver(); getWorld().addObject(gameOver, getWorld().getWidth()/2, getWorld().getHeight()/2); //getWorld().removeObject(Mario.class); getWorld().removeObject(this); Greenfoot.playSound("mariodie.wav"); Greenfoot.stop(); } } private void gameEnd() { Greenfoot.stop(); } }