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

2015/5/2

Keep object when moving to new world

1
2
3
Linkk0 Linkk0

2015/5/2

#
Hi there, I currently have a game where the player moves into new areas, but I am unsure of how to keep the same player object, and amount of lives in each new area. My level transition is inside my player object
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
public void level_right()
{
    if (getX() == 959)
    {
        level = level +1;
    }
     
    if (getY() == 639)
    {
        level = level +2;
    }
}
 
public void new_level()
{
    if (level == 2)
    {
        Second_world second = new Second_world(); //Create the end game scenario
        Greenfoot.setWorld(second); //Set the world for the End Game World
        setImage (sr);
    }
     
         
    if (level == 3)
    {
        Third_World third = new Third_World(); //Create the end game scenario
        Greenfoot.setWorld(third); //Set the world for the End Game World
    }
     
     
}
And my objects are inside my initial world 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
*/
 
public class world extends World
{
 
    /**
     * Constructor for objects of class world.
     *
     */
    public world()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(960, 640, 1);
 
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Player player = new Player();
        addObject(player, 130, 262);
        Lives lives = new Lives();
        addObject(lives, 61, 40);
        lives.setLocation(87, 34);
        Enemy enemy = new Enemy();
        addObject(enemy, 406, 198);
        PowerUp powerup = new PowerUp();
        addObject(powerup, 400, 303);
        Earth_Boss earth_boss = new Earth_Boss();
        addObject(earth_boss, 575, 420);
    }
     
    public Lives lives = new Lives();
}
Could someone please provide help as to how I would be able to keep my player and lives when moving through to different rooms. Thanks
lordhershey lordhershey

2015/5/3

#
There are lots of ways, but in this case I would have multiple constructors, the first world would be the only one to use the default constructor, the others woul take the player object and the lives object as arguments. I would put that lives = new Lives() in the public constructor of the first world. I would have code, but I am using a mobile device atm. :/
Linkk0 Linkk0

2015/5/3

#
Would that mean I would have to make the worlds subclasses of an initial world. I was wanting the ability to move back and forth between all of the worlds, so in that case, to avoid creating a new player everytime I go back to the first world, it would be called from the top level class. How would the initial lives class be updated every time it went into a new world from the previous one, rather than the initial one too?
lordhershey lordhershey

2015/5/3

#
Some thing kind of like this:
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
56
57
58
public class world extends World
{
  
    /**
     * Constructor for objects of class world.
     *
     */
    public world(Player p,int px, int py,Lives l)
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(960, 640, 1);
  
        prepare();
         
        if(null == p)
        {
            Player player = new Player();
            addObject(player, 130, 262);
        }
        else
        {
            addObject(p,px,py);
        }
         
        if(null == l)
        {
            lives = new Lives();
        }
        else
        {
            lives = l;
        }      
        addObject(lives, 61, 40);
        lives.setLocation(87, 34);
 
    }
  
    public world()
    {
        this(null,null);
    }
  
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Enemy enemy = new Enemy();
        addObject(enemy, 406, 198);
        PowerUp powerup = new PowerUp();
        addObject(powerup, 400, 303);
        Earth_Boss earth_boss = new Earth_Boss();
        addObject(earth_boss, 575, 420);
    }
      
    public Lives;
}
you call new world() on the first use of this , then when you want to switch back use new world(p,p.getX(),p.getY(),l) This is not always the best way, but this might work.
Linkk0 Linkk0

2015/5/3

#
I was getting an error stating that public Lives needed an identifier, so I changed the earlier mentions to Lives lives = new lives and the same for Lives lives = 1, which has solved the problem so far but I don't know if that means that it is going to cause an issue when calling to a new world? I am also getting an error in the public world() stating that there is no suitable constructor, any ideas? Thank so much for the help so far!
yedefei yedefei

2015/5/3

#
aha~I‘ve just maken a scenerio about this issue :http://www.greenfoot.org/scenarios/13735 ,where I keep the references of all the world and objects ,so you needn't make a new instance of world or player when switching world 。 maybe you could get any inspiration from it。
Linkk0 Linkk0

2015/5/3

#
Unfortunately I had seen the original version of that game and I don't think it fits with what I want, as I will be added more area that will have multiple entrances to them, so it can't be update only from a singular scene, plus my level transitions are set within my player class rather than the world.
danpost danpost

2015/5/3

#
Maybe my Super Level Support Class might come in handy here. Just have your level worlds extend the 'Level' support class and add your main actor to the declared fields and objects that are shared among your levels in the Level world code. You can use it as a guide to create your own specialized class for your scenario or use it directly.
yedefei yedefei

2015/5/4

#
Linkk0 wrote...
Unfortunately I had seen the original version of that game and I don't think it fits with what I want, as I will be added more area that will have multiple entrances to them, so it can't be update only from a singular scene, plus my level transitions are set within my player class rather than the world.
I've updated my scenerio where has two entrances to the other world and uses static field variables to keep the reference of world and player。
Linkk0 Linkk0

2015/5/4

#
danpost - I tried using your code as help but I can't get it to work as i have my lives (score) set up as an actor rather than in the world, When I was able to get the level transition to work it was messing up my code for other objects that related to my player character, and i can't understand how to make the actors static so they can keep anything that has happened to them. yedefei - I have tried using your code and I am encountering errors because the world transition is object specific rather than area specific (e.g player reaches point x=0) Sorry for all the issues guys I am just getting to the point of sheer frustration with this
danpost danpost

2015/5/4

#
If you show what you have tried, instead of just saying you tried this or that, we could help fix the code.
Linkk0 Linkk0

2015/5/4

#
The problem is it is set across a couple of different actors. This is the one that i have tried using your code
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import greenfoot.*;
 
/**
 * AUTHOR: danpost (greenfoot.org username)
 * DATE: February 11, 2014
 * MODIFIED: June 17, 2014 (minor changes and finished documentation)
 *           August 8, 2014 (made some methods static and added the 'startOver' method)
 *
 * CLASS: Level -- a superclass to assist in many aspects of a world level changing scenario.
 * (1) provides an easy splash screen
 * (2) provides score and level fields whose values are not lost between levels
 * (3) provides display of the score and level values
 * (4) provides easy access to the 'nextLevel' and 'resetLevel' methods of your sub-world levels (see Bug class code)
 * (5) provides a place for any fields and methods that are shared among all your sub-world levels (such as 'String[] map',
 * which can be assigned in the 'setFields' method of that subclass, or 'public void createMap()' which need only be
 * placed in this class).
 */
public class Level extends World
{
    static boolean started; // to start level one using the 'started' method only when reset or compiled
    static int score; // score
    static int level; // level
    static Actor scoreText = getNewStillActor(); // to display score
    static Actor levelText = getNewStillActor(); // to display level
    // declare all additional fields shared among your subclasses here   
     
    /**
     * This initial constructor creates a world that provides a splash screen for your project and
     * resets the static fields to start/reset the project
     */
    public Level()
    {
        super(800, 600, 1); // set dimensions to largest x and largest y used in subworlds
        started = false; // initialize started (allows 'started' method to start level one)
        score = 0; // initialize score
        level = 0; // initialize level
    }
     
    /**
     * Level Constructor: calls Level(int, int, int, boolean) constructor with default bounding as true
     *
     * @param w the width of (or number of cells across) the world as an integer value
     * @param h the height of (or number of cells down) the world as an integer value
     * @param c the cellsize (width and height of a single cell) as an integer value
     */
    public Level(int w, int h, int c)
    {
        this(w, h, c, true);
    }
     
    /**
     * This main constructor creates the common objects and sets the field values; common steps in construction among
     * all levels can be appended to the code; steps that are not common to all levels should be done in the constructor
     * of those particular levels
     *
     * @param w the width of (or number of cells across) the world as an integer value
     * @param h the height of (or number of cells down) the world as an integer value
     * @param c the cellsize (width and height of a single cell) as an integer value
     * @param b the bounding flag (whether the world is bounded or not) as a boolean value
     */
    public Level(int w, int h, int c, boolean b)
    {
        super(w, h, c, b);
        adjustScore(0); // sets display of score without changing the current score
        addObject(scoreText, 75, 20); // adds score display to world
        setFields(); // sets the value of all additional shared fields declared above
    }
     
    // private void createMap() // method to read map and create and add actors to the world here;
    // download and view the code of the 'Platformer Tutorial' scenario by askGriff to see how mapping would work with this
    // (the Level class there was a pre-cursor to this)
 
     
    /**
     * This method controls the proper resetting/compiling of the project; only modify, if required,
     * the initial level world subclass name ('Level1'); a Menu world (as a subclass of World) could be
     * started here instead of going directly to the Level1 world
     */
    public void started()
    {
        if (!started)
        {
            startOver();
            started = true;
        }
    }
     
    /**
     * Use this class method to restart the scenario back at the first level (or menu); only modify, if required,
     * the level world subclass name ('Level1'); a Menu world (as a subclass of World) could be initial started
     * here instead of going directly to the Level1 world; you may use 'Level.startOver();' from anywhere
     */
    public static void startOver()
    {
        Greenfoot.setWorld(new Level1());
    }
     
    /**
     * This internal class method is used to set/change the display of the level number; modify the image as needed
     */
    private static void setLevelText()
    {
        levelText.setImage(new GreenfootImage("Level: "+level, 24, null, null));
    }
     
    /**
     * Use this method to change the score (or not) and re-display it; modify the image as needed;
     * you may use 'Level.adjustScore(nn);' from anywhere
     *
     * @param adjustment the change in amount, if any, in the 'score' value
     */
    public static void adjustScore(int adjustment)
    {
        score += adjustment;
        scoreText.setImage(new GreenfootImage("Score: "+score, 24, null, null));
    }
 
    /**
     * Create methods in all sub-classes with this name, if needed, to assign values to additional common fields declared above;
     * DO NOT modified or remove this method
     */
    public void setFields(){} // include this method in all subclasses with appropriate code (see subclasses)
     
    /**
     * Create methods in all sub-classes with this name to transfer to the next level from that level;
     * for any actor in any level world, you may use '((Level)getWorld()).nextLevel()';
     * DO NOT modified or remove this method
     */
    public void nextLevel(){} // include this method in all subclasses with appropriate code (see subclasses)
     
    /**
     * Create methods in all sub-classes with this name to reset the current level from that level;
     * for any actor in any level world, you may use '((Level)getWorld()).resetLevel();'
     * DO NOT modified or remove this method
     */
    public void resetLevel(){} // include this method in all subclasses with appropriate code (see subclasses)
     
    /**
     * Use this method to create and return an actor that is protected from movement commands;
     * (already used for the actors that display the score and level text)
     * DO NOT modified or remove this method
     */
    public static Actor getNewStillActor()
    {
        return new Actor()
        {
            public void setLocation(int x, int y){}
            public void move(int n){}
        };
    }
     
    /**
     * Demo act method: this can be removed or revised as needed; added to give all actors a downward pull;
     * (notice how score and level text fields are pinned in place)
     */
    public void act()
    {
        for (Object obj : getObjects(null))
        {
            Actor actor = (Actor)obj;
            actor.setLocation(actor.getX(), actor.getY());
        }
    }
}
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
import greenfoot.*;
 
public class Level1 extends Level
{
    public Level1()
    {
        super(800, 600, 1);
        addObject(new Player(), getWidth()/2, getHeight()/2);
    }
     
    public void setFields()
    {
        level = 1;
    }
     
    public void nextLevel()
    {
        Greenfoot.setWorld(new Level2());
    }
     
    public void resetLevel()
    {
        Greenfoot.setWorld(new Level1());
    }
}
However in this case if I try and pick up an object, such as a health increase I get an error
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
import greenfoot.*;
 
/**
 * 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()
    {
        lookForPlayer();
    }   
     
        public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;       
    }
     
        public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor); //Removes an intersecting actor from the world
        }
    }
     
    public void lookForPlayer()
    {
        if ( canSee(Player.class) )
        {
            Lives lives = (Lives) getWorld().getObjects(Lives.class).get(0); 
            lives.livesUp();
            Level.adjustScore(1);
            getWorld().removeObject(this);
        }
    }
}
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
56
57
58
59
60
61
62
63
64
65
66
import greenfoot.*; 
import java.awt.Color; 
    
public class Lives extends Actor 
    GreenfootImage sr = new GreenfootImage ("Heart Full.png");
    int lives = 2;
    int maxLives = 3;
            
    public Lives() 
    
        updateBoard(); 
    
     
    public void maxLives()
    {
        if (lives > maxLives)
        {
            lives = maxLives;
        }
    }
     
    private void updateBoard() 
    
        setImage(new GreenfootImage("Lives remaining: " + lives, 20, Color.black, new Color(0, 0, 0, 0)));
    
        
    public void add(int addVal) 
    
        if (lives == 0 && addVal == -1
        { // ^ (lost a life and no lives remaining) 
            Greenfoot.stop(); 
            return
        
        lives += addVal; updateBoard(); 
    
        
    public int getLivesLeft() 
    
        return lives; 
    
      
    public void livesDown()
    {
  
        lives = lives -1;
        updateBoard();
        if( lives == 0)
        {           
            Greenfoot.stop(); 
            return;
        }         
    }
     
    public void livesUp()
    {
        lives = lives +1;
        maxLives();
        updateBoard();
    }
     
        public void maxLivesUp()
    {
        maxLives = maxLives +1;
    }
}
Linkk0 Linkk0

2015/5/4

#
Linkk0 wrote...
The problem is it is set across a couple of different actors. This is the one that i have tried using your code
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import greenfoot.*;
 
/**
 * AUTHOR: danpost (greenfoot.org username)
 * DATE: February 11, 2014
 * MODIFIED: June 17, 2014 (minor changes and finished documentation)
 *           August 8, 2014 (made some methods static and added the 'startOver' method)
 *
 * CLASS: Level -- a superclass to assist in many aspects of a world level changing scenario.
 * (1) provides an easy splash screen
 * (2) provides score and level fields whose values are not lost between levels
 * (3) provides display of the score and level values
 * (4) provides easy access to the 'nextLevel' and 'resetLevel' methods of your sub-world levels (see Bug class code)
 * (5) provides a place for any fields and methods that are shared among all your sub-world levels (such as 'String[] map',
 * which can be assigned in the 'setFields' method of that subclass, or 'public void createMap()' which need only be
 * placed in this class).
 */
public class Level extends World
{
    static boolean started; // to start level one using the 'started' method only when reset or compiled
    static int score; // score
    static int level; // level
    static Actor scoreText = getNewStillActor(); // to display score
    static Actor levelText = getNewStillActor(); // to display level
    // declare all additional fields shared among your subclasses here   
     
    /**
     * This initial constructor creates a world that provides a splash screen for your project and
     * resets the static fields to start/reset the project
     */
    public Level()
    {
        super(800, 600, 1); // set dimensions to largest x and largest y used in subworlds
        started = false; // initialize started (allows 'started' method to start level one)
        score = 0; // initialize score
        level = 0; // initialize level
    }
     
    /**
     * Level Constructor: calls Level(int, int, int, boolean) constructor with default bounding as true
     *
     * @param w the width of (or number of cells across) the world as an integer value
     * @param h the height of (or number of cells down) the world as an integer value
     * @param c the cellsize (width and height of a single cell) as an integer value
     */
    public Level(int w, int h, int c)
    {
        this(w, h, c, true);
    }
     
    /**
     * This main constructor creates the common objects and sets the field values; common steps in construction among
     * all levels can be appended to the code; steps that are not common to all levels should be done in the constructor
     * of those particular levels
     *
     * @param w the width of (or number of cells across) the world as an integer value
     * @param h the height of (or number of cells down) the world as an integer value
     * @param c the cellsize (width and height of a single cell) as an integer value
     * @param b the bounding flag (whether the world is bounded or not) as a boolean value
     */
    public Level(int w, int h, int c, boolean b)
    {
        super(w, h, c, b);
        adjustScore(0); // sets display of score without changing the current score
        addObject(scoreText, 75, 20); // adds score display to world
        setFields(); // sets the value of all additional shared fields declared above
    }
     
    // private void createMap() // method to read map and create and add actors to the world here;
    // download and view the code of the 'Platformer Tutorial' scenario by askGriff to see how mapping would work with this
    // (the Level class there was a pre-cursor to this)
 
     
    /**
     * This method controls the proper resetting/compiling of the project; only modify, if required,
     * the initial level world subclass name ('Level1'); a Menu world (as a subclass of World) could be
     * started here instead of going directly to the Level1 world
     */
    public void started()
    {
        if (!started)
        {
            startOver();
            started = true;
        }
    }
     
    /**
     * Use this class method to restart the scenario back at the first level (or menu); only modify, if required,
     * the level world subclass name ('Level1'); a Menu world (as a subclass of World) could be initial started
     * here instead of going directly to the Level1 world; you may use 'Level.startOver();' from anywhere
     */
    public static void startOver()
    {
        Greenfoot.setWorld(new Level1());
    }
     
    /**
     * This internal class method is used to set/change the display of the level number; modify the image as needed
     */
    private static void setLevelText()
    {
        levelText.setImage(new GreenfootImage("Level: "+level, 24, null, null));
    }
     
    /**
     * Use this method to change the score (or not) and re-display it; modify the image as needed;
     * you may use 'Level.adjustScore(nn);' from anywhere
     *
     * @param adjustment the change in amount, if any, in the 'score' value
     */
    public static void adjustScore(int adjustment)
    {
        score += adjustment;
        scoreText.setImage(new GreenfootImage("Score: "+score, 24, null, null));
    }
 
    /**
     * Create methods in all sub-classes with this name, if needed, to assign values to additional common fields declared above;
     * DO NOT modified or remove this method
     */
    public void setFields(){} // include this method in all subclasses with appropriate code (see subclasses)
     
    /**
     * Create methods in all sub-classes with this name to transfer to the next level from that level;
     * for any actor in any level world, you may use '((Level)getWorld()).nextLevel()';
     * DO NOT modified or remove this method
     */
    public void nextLevel(){} // include this method in all subclasses with appropriate code (see subclasses)
     
    /**
     * Create methods in all sub-classes with this name to reset the current level from that level;
     * for any actor in any level world, you may use '((Level)getWorld()).resetLevel();'
     * DO NOT modified or remove this method
     */
    public void resetLevel(){} // include this method in all subclasses with appropriate code (see subclasses)
     
    /**
     * Use this method to create and return an actor that is protected from movement commands;
     * (already used for the actors that display the score and level text)
     * DO NOT modified or remove this method
     */
    public static Actor getNewStillActor()
    {
        return new Actor()
        {
            public void setLocation(int x, int y){}
            public void move(int n){}
        };
    }
     
    /**
     * Demo act method: this can be removed or revised as needed; added to give all actors a downward pull;
     * (notice how score and level text fields are pinned in place)
     */
    public void act()
    {
        for (Object obj : getObjects(null))
        {
            Actor actor = (Actor)obj;
            actor.setLocation(actor.getX(), actor.getY());
        }
    }
}
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
import greenfoot.*;
 
public class Level1 extends Level
{
    public Level1()
    {
        super(800, 600, 1);
        addObject(new Player(), getWidth()/2, getHeight()/2);
    }
     
    public void setFields()
    {
        level = 1;
    }
     
    public void nextLevel()
    {
        Greenfoot.setWorld(new Level2());
    }
     
    public void resetLevel()
    {
        Greenfoot.setWorld(new Level1());
    }
}
However in this case if I try and pick up an object, such as a health increase I get an error
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
import greenfoot.*;
 
/**
 * 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()
    {
        lookForPlayer();
    }   
     
        public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;       
    }
     
        public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor); //Removes an intersecting actor from the world
        }
    }
     
    public void lookForPlayer()
    {
        if ( canSee(Player.class) )
        {
            Lives lives = (Lives) getWorld().getObjects(Lives.class).get(0); 
            lives.livesUp();
            Level.adjustScore(1);
            getWorld().removeObject(this);
        }
    }
}
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
56
57
58
59
60
61
62
63
64
65
66
import greenfoot.*; 
import java.awt.Color; 
    
public class Lives extends Actor 
    GreenfootImage sr = new GreenfootImage ("Heart Full.png");
    int lives = 2;
    int maxLives = 3;
            
    public Lives() 
    
        updateBoard(); 
    
     
    public void maxLives()
    {
        if (lives > maxLives)
        {
            lives = maxLives;
        }
    }
     
    private void updateBoard() 
    
        setImage(new GreenfootImage("Lives remaining: " + lives, 20, Color.black, new Color(0, 0, 0, 0)));
    
        
    public void add(int addVal) 
    
        if (lives == 0 && addVal == -1
        { // ^ (lost a life and no lives remaining) 
            Greenfoot.stop(); 
            return
        
        lives += addVal; updateBoard(); 
    
        
    public int getLivesLeft() 
    
        return lives; 
    
      
    public void livesDown()
    {
  
        lives = lives -1;
        updateBoard();
        if( lives == 0)
        {           
            Greenfoot.stop(); 
            return;
        }         
    }
     
    public void livesUp()
    {
        lives = lives +1;
        maxLives();
        updateBoard();
    }
     
        public void maxLivesUp()
    {
        maxLives = maxLives +1;
    }
}
I am wanting to edit the 'score' to be the same as the lives, even if i got the pickup to work here I also assume that it still wouldnt work right as the lives would still reset in every room. Sorry about that double post there, I tried to edit and it all came up again.
danpost danpost

2015/5/4

#
It is not the Health or Lives class that need work here. It is your world classes that need work. Also, you need to add static references to those objects you wish to pass from level to level in the Level class so your subworlds can add them into their domain.
danpost danpost

2015/5/4

#
For example, to have the same Player object used in all worlds the player is in:
1
2
3
4
5
6
// in Level class (around line 25)
static Player player; // to reference the player
// in Level class constructor (around line 37)
player = new Player(); // to initialize the player
// in the constructor of each subworld the player is in
addObect(player, getWidth()/2, getHeight()/2);
There are more replies on the next page.
1
2
3