In my game, you are supposed to kill all the enemies and go through doors to get to another world. However, that new world is the same world as the starting world, just a new instance. However, when i go through a door, the Health of the player resets. How do i make the health the same as the health in the previous world?
I am new to Java and Greenfoot so my code may be a bit confusing/sloppy. I have posted the World, Health, Player and DoorPlayer:
And the Door:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class door extends Actor
{
World BigWorld = getWorld();
MyWorld myWorld = (MyWorld)BigWorld;
public void act() {
if (isTouching(Player.class)){
Greenfoot.setWorld(new MyWorld());
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) World: /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { int count = 0; int time = 0; int spawnSpeed = 50; int randomSpawn = Greenfoot.getRandomNumber(4); int randInt = Greenfoot.getRandomNumber(900); int randFloat = Greenfoot.getRandomNumber(950); int randomNum1 = Greenfoot.getRandomNumber(10); int Loop = 0; public MyWorld world; public Player mainPlayer = new Player(); public door Door = new door(); Health healthbar = new Health(); private Health health; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(700, 700, 1); addObject(mainPlayer, getWidth()/2, getHeight()/2); randomNum1 = Greenfoot.getRandomNumber(11) + 5; addObject(healthbar, 28, 10); } public void update(){ spawnEnemy(); count++; if (count == randomNum1){ count += count; } Loop++; randInt = Greenfoot.getRandomNumber(700); randFloat = Greenfoot.getRandomNumber(700); newLevel(); time++; } public Health getHealthBar(){ return healthbar; } public void act(){ update(); } public void spawnEnemy(){ if (count == Loop){ randomSpawn = Greenfoot.getRandomNumber(4); switch(randomSpawn){ case 1 : addObject(new ENEMY(mainPlayer), randInt, randFloat); break; case 2 : addObject(new ENEMY(mainPlayer), randInt, randFloat); break; case 3 : addObject(new ENEMY(mainPlayer), randInt, randFloat); break; case 4 : addObject(new ENEMY(mainPlayer), randInt, randFloat); break; } } } public void newLevel(){ if (getObjects(ENEMY.class).isEmpty() && count >= 100){ addObject(Door, randInt, randFloat);} } public Player getPlayer() { return mainPlayer; } } Health:
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 Player { int health = 100; public Health() { setImage(new GreenfootImage(62, 17)); getImage().drawRect(0,0,51,11); getImage().setColor(Color.GREEN); getImage().fillRect(1,1,health * 10,10); } public void act() { setImage(new GreenfootImage(102, 26)); getImage().drawRect(0,0,101,25); getImage().setColor(Color.GREEN); getImage().fillRect(1,1,health,25); World world = getWorld(); } public void loseHealth() { health--; } }
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 { /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ boolean stunned = false; boolean attacked = false; boolean hitByFoe = false; int time = 0; int ScoreTime = 0; public void act() { ScoreTime++; if (stunned == false){ if (Greenfoot.isKeyDown("w")){ setLocation​(getX(), getY() - 3); } if (Greenfoot.isKeyDown("s")){ setLocation (getX(), getY() + 3); } if (Greenfoot.isKeyDown("a")){ setLocation (getX() - 3, getY()); int rotation = 1; } if (Greenfoot.isKeyDown("d")){ setLocation (getX() + 3, getY()); int rotation = 2; } } String space = Greenfoot.getKey(); if (space != null){ if (attacked == false){ if (space.equals("space")){ Sword s = new Sword(); getWorld().addObject(s, getX()-5, getY()+10); stunned = true; attacked = true; time = 0; } } } if (isTouching(Sword.class)){ stunned = true; } else{ stunned = false; } if (attacked == true){ time++; if (time == 100){ attacked = false; } } hitByEnemy(); } public void hitByEnemy() { Actor enemy = getOneObjectAtOffset(0, 0,ENEMY.class); Actor slime = getOneObjectAtOffset(0, 0,Slime.class); if(enemy!=null || slime!=null) { World BigWorld = getWorld(); MyWorld myWorld = (MyWorld)BigWorld; Health healthbar = myWorld.getHealthBar(); if (hitByFoe == false){ healthbar.loseHealth(); hitByFoe = true; if(healthbar.health <= 0){ } } else hitByFoe = false; } } }