This site requires JavaScript, please enable it in your browser!
Greenfoot back
ReasonedTiger39
ReasonedTiger39 wrote ...

2023/3/4

How do i carry the variable health from one world to a new instance of the same world?

ReasonedTiger39 ReasonedTiger39

2023/3/4

#
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 Door
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--;
    }
}
Player:
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;
        }
    }
}
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()); } } }
danpost danpost

2023/3/4

#
ReasonedTiger39 wrote...
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? << Codes Omitted >>
Replace lines 31 thru 34 in your MyWorld class above with:
public MyWorld(Player player)
{
    super(700, 700, 1);
    mainPlayer = player;
Then, add the following to the MyWorld class:
public MyWorld()
{
    this(new Player());
}
Finally, when creating a new world, use one of the following: new MyWorld(mainPlayer) // from MyWorld class new MyWorld(this) // from Player class new MyWorld(((MyWorld)getWorld()).mainPlayer) // from any other Actor subclass For conciseness (this will not affect anything after the changes above are done), you can change line 21 in the MyWorld class above to:
public Player mainPlayer;
You need to login to post a reply.