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
danpost danpost

2015/5/4

#
You evidentally clicked on 'Quote' instead of 'Edit'. Once you have the Lives object set up similar to how I show the Player object above, it will not be 'resetting'. What you were seeing is not the object being reset, but the object being replaced with a new one (with initial set-up).
Linkk0 Linkk0

2015/5/4

#
I've not really worked with static before. I think that I know where the issue is with one part of it but i'm not quite sure how to fix it.
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
165
166
167
168
169
170
171
172
173
174
175
176
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 Actor lives = getNewStillActor(); // to display score
    static Actor player = 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(960, 640, 1); // set dimensions to largest x and largest y used in subworlds
        started = false; // initialize started (allows 'started' method to start level one)
    }
     
    /**
     * 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);
        setLives(); // sets text display of level
        addObject(lives, 75, 20); // add level display to world
        setFields(); // sets the value of all additional shared fields declared above
        setPlayer(); // sets text display of level
        addObject(player, w-75, 20); // add level display to world
        // createMap() can be called here if using maps with the added method below
        showInstructions(); // example of added method call that is shared by all levels
    }
     
    // 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)
     
    /**
     * Example of added method
     */
    private void showInstructions()
    {
        String text = "Use arrow keys to move ladybug";
        text += "\n\nClick on ladybug to execute 'adjustScore(1)'";
        text += "\n\nMove ladybug to left edge to execute 'resetLevel'";
        text += "\n\nMove ladybug to right edge to execute 'nextLevel'";
        text += "\n(or terminate on level 3))";
        GreenfootImage textImg = new GreenfootImage(text, 24, java.awt.Color.black, null);
        getBackground().drawImage(textImg, (getWidth()-textImg.getWidth())/2, (getHeight()-textImg.getHeight())/2);
    }
     
    /**
     * 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 setPlayer()
    {
        player = new Player();
    }
     
    /**
     * 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 setLives()
    {
        lives = new Lives();
    }
 
    /**
     * 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());
        }
    }
}
I have created a static reference to the player and the lives, but the only was I know to add them in is to make the player reference a new player, but this will make the player a new one again every level I move into if I'm right? how would I fix that so it is only when I start the game the actor is newly created? Sorry I hadn't notcied you other post, I will jsut check it over now thanks.
Linkk0 Linkk0

2015/5/4

#
When I try to put it in like you suggested when I input addObject player to the level1 class I am getting an error box up saying java.lang.NullPointerException
danpost danpost

2015/5/4

#
Your initial Level class should not have all the stuff that I used in the particular scenario you pulled the class from. Stripped down, it should start something 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
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
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.
 */
public class Level extends World
{
    static boolean started; // to start level one using the 'started' method only when reset or compiled
    static Lives lives; // to retain the Lives object
    static Player player; // to retain the Player object
      
    /**
     * 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(960, 640, 1); // set dimensions to largest x and largest y used in subworlds
        started = false; // initialize started (allows 'started' method to start level one)
        player = new Player(); // to create new Player object when project is reset
        lives = new Lives(); // to create new Lives object when project is reset
     }
      
    /**
     * 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);
        setFields(); // sets the value of all additional shared fields declared above
        addObject(lives, 75, 20); // add Lives object into world
        addObject(player, w/2, h/2); // add Player object into world
    }
     
    /**
     * 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());
    }
      
  
    /**
     * 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)
}
Now, I have the player being added in the middle of the world in this class; so, if that is where it goes in all sub-levels, you can remove the statements that adds the player into the world from all sub-world codes.
Linkk0 Linkk0

2015/5/4

#
If I remove the player from the sub classes it does not show on the world, and the lives are still moving back to the initial number when I move screen unforutnately. I also tried adding the static references to the lives and player to my original code, and it has the same issue or moving back to the original number, but it is showing up with just the 'addObject' in the sub worlds
danpost danpost

2015/5/4

#
Please show the code you are using (world class codes of subclasses of Level). Cannot help without it.
Linkk0 Linkk0

2015/5/4

#
I am using both the code that you just showed there and :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Main_World here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Main_World extends World
{
    static Player player; // to reference the player
    static Lives lives;
    /**
     * Constructor for objects of class Main_World.
     *
     */
    public Main_World()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(960, 640, 1);
        player = new Player(); // to initialize the player
        lives = new Lives();
    }
}
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class A1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class A1 extends Main_World
 
{
     
    /**
     * Constructor for objects of class world.
     *
     */
    public A1()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
         
        prepare();
        addObject(player, 120, 120);
        addObject(lives, 89, 40);
         
    }
 
    /**
     * 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);
        Earth_Boss earth_boss = new Earth_Boss();
        addObject(earth_boss, 575, 420);
        Mana mana = new Mana();
        addObject(mana, 106, 95);
        mana.setLocation(89, 65);
        Water_Artefact water_artefact = new Water_Artefact();
        addObject(water_artefact, 190, 375);
        Mana_Powerup mana_powerup = new Mana_Powerup();
        addObject(mana_powerup, 224, 195);
        removeObject(mana_powerup);
        Earth_Artefact earth_artefact = new Earth_Artefact();
        addObject(earth_artefact, 175, 449);
        Fire_Artefact fire_artefact = new Fire_Artefact();
        addObject(fire_artefact, 180, 548);
        Health health = new Health();
        addObject(health, 231, 308);
        Mana_Potion mana_potion = new Mana_Potion();
        addObject(mana_potion, 226, 244);
    }
 
     
     
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class A2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class A2 extends Main_World
{
 
    /**
     * Constructor for objects of class Second_world.
     *
     */
    public A2()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        addObject(lives, 61, 40);
        addObject(player, 73, 305);
    }
}
But that was just as a test to see if I could get it working there.
danpost danpost

2015/5/4

#
In Main_World class, remove lines 11, 12, 21 and 22. Also, change line 9 to this:
1
public class Main_World extends Level
In A1 class, change lines 22 and 23 to this:
1
2
addObject(Level.player, 120, 120);
addObject(Level.lives, 89, 40);
In Level class, remove lines 55 and 56.
danpost danpost

2015/5/4

#
I edited my above post. Then I noticed you had added more to yours also. In A2 class, change lines 19 and 20 to this:
1
2
addObject(Level.lives, 61, 40);
addObject(Level.player, 73, 305);
Linkk0 Linkk0

2015/5/4

#
What I mean was I was using two different version of the program, one with your code and one with my original code. That Main_World class is my version of your Level class essentially, so you can just look as one. I edited both them in the addobject of the subworld to be Level.player / Main_World.player and removed lines 55 and 56, but it's still the same issue
danpost danpost

2015/5/4

#
Linkk0 wrote...
What I mean was I was using two different version of the program, one with your code and one with my original code. That Main_World class is my version of your Level class essentially, so you can just look as one. I edited both them in the addobject of the subworld to be Level.player / Main_World.player and removed lines 55 and 56, but it's still the same issue
Then you need to add the other constructors to the Main_World class as shown in the Level class. Also, your A1 and A2 class constructors need 'super' calls added at the beginning of them.
danpost danpost

2015/5/4

#
For my information, do me a favor and perform the following actions: (1) compile the project (2) right click on world background (3) select 'Inspect' (click on) (4) report title of pop-up (at top) back to me
danpost danpost

2015/5/4

#
Place a copy of the 'startOver' method of the Level class in your Main_World class and change 'new Level1()' to 'new A1()'.
Linkk0 Linkk0

2015/5/4

#
If I try and put super(960, 640, 1) in to the sub classes I am getting an error message. Ive added the startOver and updated it Title of pop-up level: Level Title of pop-up main_world: Main World Do you want me to find somewhere to upload the file to so you can have a look at it?
danpost danpost

2015/5/4

#
If you can compile it, you can upload it here on the site -- call it something like 'Broken Game-WIP'. Make sure to check the 'Publish souce code' checkbox.
There are more replies on the next page.
1
2
3