I have a World superclass and 4 world subclasses. How do I make it so when I loose health it saves when I load the next level? I've been trying to save my StarPower and Health to the Level class But I don't really know where to start.
My Level Class:
My World 3 Class:
My StarPower Class:
My Health Class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Level here. * * @author (your name) * @version (a version number or a date) */ public class Level extends World { private static int starPower; private static int health; private int add; /** * Constructor for objects of class Level. * */ public Level() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(750, 400, 1); } public void setStarPower(int add) { starPower = starPower + add; } public void getStarPower() { return starPower; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class World3 here. * * @author (your name) * @version (a version number or a date) */ public class World3 extends Level { /** * Constructor for objects of class World3. * */ public World3() { prepare(); } public void prepare() { Board board = new Board(); addObject(board, 560, 280); MainCharacter maincharacter = new MainCharacter(); addObject(maincharacter, 551, 189); board.setLocation(343, 270); board.setLocation(375, 270); maincharacter.setLocation(700, 189); Level3 level3 = new Level3(); addObject(level3, 45, 239); level3.setLocation(45, 235); Info3 info3 = new Info3(); addObject(info3, 338, 323); info3.setLocation(338, 323); Health health = new Health(); addObject(health, 40, 35); health.setLocation(40, 35); int level = 3; Star star = new Star(); addObject (star, 333, 302); star.setLocation(333,302); Button button = new Button(); addObject(button, 105,100); button.setLocation(105,100); Board4 board4 = new Board4(); addObject(board4,341, 346); board4.setLocation(341, 346); Respawn respawn = new Respawn(); addObject(respawn, 300, 399); respawn.setLocation(300,399); StarPower starpower = new StarPower(); addObject(starpower, 520, 30); starpower.setLocation(520, 30); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Font; import java.awt.Color; /** * Write a description of class StarPower here. * * @author (your name) * @version (a version number or a date) */ public class StarPower extends Actor { /** * Act - do whatever the StarPower wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { changePower(); getStarPower(); draw(); } private int starpwr = 0; public String starPower = "Star Power: "+ starpwr; public void draw() { starPower = "Star Power: "+ starpwr; setImage(new GreenfootImage(starPower, 25, Color.BLACK, Color.WHITE)); Level.setStarPower().add(1); } public int points = 0; public void setStar(int points) { starpwr = starpwr + points; } public int getStarPower() { return starpwr; } public void changePower(){ } public void resetPower() { starpwr = 0; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Health here. * * @author (your name) * @version (a version number or a date) */ public class Health extends Actor { /** * Act - do whatever the Health wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { healthStatus(); } private int health = 4; public void setHealth(int points) { health = health + points; } public int getHealth() { return health; } public void healthStatus() { switch (health) { case 1: setImage("health25.png"); break; case 2: setImage("health50.png"); break; case 3: setImage("health75.png"); break; case 4: setImage("healthMax.png"); break; default:break; } if(health <= 0) { World1 world1 = new World1(); Greenfoot.setWorld(world1); health = 4; StarPower starpower = (StarPower)getWorld().getObjects(StarPower.class).get(0); starpower.resetPower(); } } }