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

2013/5/8

New World Help

1
2
3
SWAG SWAG

2013/5/11

#
danpost wrote...
Create a new sub-class of World called 'Level'. The code below is a general outline of what it should consist of. Then, make all the worlds that the player and counter will be in extend this 'Level' world. The fields declared in the 'Level' world should not be declared in those sub-classes; only used by them, including adding the objects into those worlds. Use the methods given directly in the sub-worlds. To reference these objects in other classes, use dot notation on the class name 'Level' (example: 'Level.getScoreCounter().add(5);').
import greenfoot.*;

public class Level extends World
{
    private static int levelNum;
    private static Player player = new Player();
    private static Counter scoreCounter = new Counter("Score:");

    public Level(int wide, int high, int size)
    {
        super(wide, high, size);
    }

    public static Counter getScoreCounter()
    {
        return scoreCounter;
    }

    public static Player getPlayer()
    {
        return player;
    }

    public static int getLevel()
    {
        return levelNumber;
    }

    public static void changeLevel(int difference)
    {
        levelNumber += difference;
        switch (levelNumber)
        {
            case 0:  Greenfoot.setWorld(new World0()); break;
            case 1:  Greenfoot.setWorld(new World1()); break;
            case 2;  Greenfoot.setWorld(new World2()); break;
            // etc.
        }
}
In the last method, replace the world names with the actual names you re using.
Can you email me for email. I will need some help with understandings this. My EMAIL: gfishy4@gmail.com
danpost danpost

2013/5/11

#
SWAG wrote...
Can you email me for email. I will need some help with understandings this. My EMAIL: gfishy4@gmail.com
I am not one to continuously check my email (maybe once or twice a week). There, however, is not really too much to understand with the code I gave. If you have any questions, asking them here should be fine.
SWAG SWAG

2013/5/11

#
danpost wrote...
SWAG wrote...
Can you email me for email. I will need some help with understandings this. My EMAIL: gfishy4@gmail.com
I am not one to continuously check my email (maybe once or twice a week). There, however, is not really too much to understand with the code I gave. If you have any questions, asking them here should be fine.
My character changes worlds when he is on the teleported and the down key is pressed. This code is in my player class. How would I switch worlds from the level class. Also how do I set the player. My player starts in different positions what code do I add in my world classes?
danpost danpost

2013/5/11

#
From the player class, just call 'Level.changeLevel(n);', replacing 'n' with the appropriate value. In the constructor of the new world (and of all secondary worlds), just use 'addObject(Level.getPlayer(), playerX, player)' and 'addObject(Level.getScoreCounter(), counterX, counterY)' to put the objects in the appropriate place in each world. Of course, replacing the object coordinate variables with the appropriate values for each world. Replacing 'n' with '0' will restart the same level; with '1' will start the next level; and with '-1' will go back to the last level.
SWAG SWAG

2013/5/11

#
danpost wrote...
From the player class, just call 'Level.changeLevel(n);', replacing 'n' with the appropriate value. In the constructor of the new world (and of all secondary worlds), just use 'addObject(Level.getPlayer(), playerX, player)' and 'addObject(Level.getScoreCounter(), counterX, counterY)' to put the objects in the appropriate place in each world. Of course, replacing the object coordinate variables with the appropriate values for each world. Replacing 'n' with '0' will restart the same level; with '1' will start the next level; and with '-1' will go back to the last level.
Thanks this all works so far, but when I reset my scenario, I get the score from previous game, and the player is from the previous games. (They don't reset.)
danpost danpost

2013/5/11

#
In the 'Level' world class, do the following:
// add the following instance field
private boolean hasStarted;
// in the constructor (after the 'super' statement)
if (!hasStarted)
{
    player = new Player();
    scoreCounter = new ScoreCounter("Score:");
    levelNumber = 0;
    hasStarted = true;
}
Then, you can remove the assignments from the field declaration statements.
// change declarations to
private Player player;
private Counter scoreCounter;
private int levelNumber;
SWAG SWAG

2013/5/11

#
danpost wrote...
In the 'Level' world class, do the following:
// add the following instance field
private boolean hasStarted;
// in the constructor (after the 'super' statement)
if (!hasStarted)
{
    player = new Player();
    scoreCounter = new ScoreCounter("Score:");
    levelNumber = 0;
    hasStarted = true;
}
Then, you can remove the assignments from the field declaration statements.
// change declarations to
private Player player;
private Counter scoreCounter;
private int levelNumber;
I get the error : non-static variable scoreCounter cannot be referenced from a static context. if I change the field declaration statements to statics the player and score doesn't carry over to next level.
danpost danpost

2013/5/11

#
Sorry, I should have copy pasted (or at least looked at the declarations before posting the change:
// change declarations to
private static Player player;
private static Counter scoreCounter;
private static int levelNumber;
SWAG SWAG

2013/5/11

#
danpost wrote...
Sorry, I should have copy pasted (or at least looked at the declarations before posting the change:
// change declarations to
private static Player player;
private static Counter scoreCounter;
private static int levelNumber;
Now the scenario works, but at world 2 the score and player are not from world 1. New ones are created and the score is at 0.
danpost danpost

2013/5/12

#
You should not be creating any Player or Counter objects anywhere but in the constructor of the Level class within the 'if' block checking if 'hasStarted' is true or not.
SWAG SWAG

2013/5/12

#
danpost wrote...
You should not be creating any Player or Counter objects anywhere but in the constructor of the Level class within the 'if' block checking if 'hasStarted' is true or not.
I'm not, in my second world I'm add them like this:
addObject(Level.getScoreCounter(), 98, 44);
        
addObject(Level.getPlayer(), 147, 32);
danpost danpost

2013/5/12

#
Oh, I guess that last boolean will not work right. We may have to create the objects in the first level sub-class and have two constructors for that class. The one you have now, which is used when starting the scenario and another to return or re-start the level during play. The second can have a single boolean parameter to signify that we are re-creating the level and that the counter and player do not need to be created again. The original one should create the objects and set 'player' and 'scoreCounter' to those new objects. So, do that; and remove 'if' block and the 'hasStarted' boolean from the Level class. Add the boolean parameter to the 'new World0()' call in the switch statement in the 'changeLevel' method.
SWAG SWAG

2013/5/12

#
danpost wrote...
Oh, I guess that last boolean will not work right. We may have to create the objects in the first level sub-class and have two constructors for that class. The one you have now, which is used when starting the scenario and another to return or re-start the level during play. The second can have a single boolean parameter to signify that we are re-creating the level and that the counter and player do not need to be created again. The original one should create the objects and set 'player' and 'scoreCounter' to those new objects. So, do that; and remove 'if' block and the 'hasStarted' boolean from the Level class. Add the boolean parameter to the 'new World0()' call in the switch statement in the 'changeLevel' method.
What Boolean parameter and where do I add it?
danpost danpost

2013/5/12

#
You do not need a class or instance field to hold this value, it is just used to ensure we are executing the alternate constructor in your first class extending Level. In the 'changeLevel' method, just change 'new World0()' with 'new World0(true)' or 'new World0(false)' . In your first sub-class that extends Level, do the following:
// change your current constructor to
public World0()
{
    super(600, 400, 1); // using your dimensions
    player = new Player();
    scoreCounter = new Counter("Score:");
    // prepare the world
}
// add the secondary constructor
public World0(boolean dummy)
{
    super(600, 400, 1);
    // prepare the world
}
The secondary constructor is used so we do not create another instance of the player or counter objects.
SWAG SWAG

2013/5/12

#
danpost wrote...
You do not need a class or instance field to hold this value, it is just used to ensure we are executing the alternate constructor in your first class extending Level. In the 'changeLevel' method, just change 'new World0()' with 'new World0(true)' or 'new World0(false)' . In your first sub-class that extends Level, do the following:
// change your current constructor to
public World0()
{
    super(600, 400, 1); // using your dimensions
    player = new Player();
    scoreCounter = new Counter("Score:");
    // prepare the world
}
// add the secondary constructor
public World0(boolean dummy)
{
    super(600, 400, 1);
    // prepare the world
}
The secondary constructor is used so we do not create another instance of the player or counter objects.
This didn't work.
There are more replies on the next page.
1
2
3