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

2014/8/17

Need Help Shifting Health Between Levels

BobFisher3 BobFisher3

2014/8/17

#
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:
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
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;
    }
}
My World 3 Class:
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
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);
    }
}
My StarPower Class:
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
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;
    }
}
My Health Class:
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
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();
        }
    }
}
danpost danpost

2014/8/17

#
In World3, and presumably in World2 and World4, you are creating a new StarPower object and adding it into your world instead of adding the one that is already referenced by your 'starPower' field. Do not create new ones in these worlds, just add the one already referenced into the world (see line 51 and 52 of the World3 code).
BobFisher3 BobFisher3

2014/8/17

#
Okay so how do I go about doing that? Do I store the current starPower in the Level class? I saw when you said:
danpost wrote...
The best way is to add a method to each and every world class that the player and score need to be in (or, if you have all those worlds sub-classed under a superclass that extends World, in the superclass):
1
2
3
4
5
6
7
public void passObjects(Player inPlayer, Score inScore)
{
    player = inPlayer;
    addObject(player, 50, 350);
    scoreCounter = inScore;
    addObject(scoreCounter, 50, 20);
}
How you go about creating the new world and pass the objects depends on your class structure. If the secondary worlds are just new instances of the original world or all the worlds the player can teleport from/to are super-classed by an intermediate class between them and the World class, then the code would be fairly straight-forward. If you have multiple worlds that all extend the World class then it gets a bit complicated.
Would I do something like this?
danpost danpost

2014/8/17

#
Maybe you are not aware of an important distinction, here. The variables being passed in the 'passObjects' method are not declared 'static' (at least, that was not the intent when I wrote that). Being your 'starPower' and 'health' fields are declared 'static', there is no need to pass them at all. You can refer to them simply as 'Level.starPower' and 'Level.health' from any class, regardless of world, if their access was declared 'public'. As 'static', these fields belong to the 'Level' class itself, and not to any specific Level world object that might be created from the class. To clarify, from any world object that is a Level object, regardless of which subclass created it, you can just refer to the fields as 'starPower' and 'health' since those classes inherit the fields and methods (public and protected) of their superclasses. You can change the access of those fields to 'protected' instead of 'private' to have access to them in your subclasses; or to 'public' if you wish access to them from any class in your project. As an alternative, you can add 'public static' "get" methods to allow other classes access. For example:
1
2
3
4
public static int getHealth()
{
    return health;
}
placing this method in the Level class would allow any class to use:
1
int health = Level.getHealth();
to get the value of the field. A "set" method would most probably also be needed, then.
BobFisher3 BobFisher3

2014/8/17

#
Thanks danpost you're an absolute legend, I understand it all now and I have managed to get everything I needed to work. Your clear instructions and information seriously sorted problems I never would have been able to. I can't thank you enough. I really appreciate your help :D
danpost danpost

2014/8/17

#
FYI, I have a Super Level Support Class you may be interested in looking over. It illustrates some of the things reviewed here.
You need to login to post a reply.