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/8

#
in my game, my character teleports to a new world. I was wonder how can I continue the score when I switch levels (I'm using the counter class). Also how would I bring my character to the new world, as the same in first world, because my character changes depending on which weapon they choose in the first level.
If you want to keep your score, the best way to do that is with the UserInfo class. The UserInfo class can hold a couple ints and Strings, including a specific int called score. All of these variables are stored on the Greenfoot servers, and there is specific info for each user of the site (thus why it's called UserInfo). What you'll have to do is when you teleport, before you switch to the new world, you must store the current score into UserInfo, and then when you open the next world, you take the score from UserInfo. More details about it can be found here: http://www.greenfoot.org/files/javadoc/greenfoot/UserInfo.html. There are also some good discussions on how to use UserInfo.
SWAG SWAG

2013/5/8

#
FlyingRabidUnicornPig wrote...
If you want to keep your score, the best way to do that is with the UserInfo class. The UserInfo class can hold a couple ints and Strings, including a specific int called score. All of these variables are stored on the Greenfoot servers, and there is specific info for each user of the site (thus why it's called UserInfo). What you'll have to do is when you teleport, before you switch to the new world, you must store the current score into UserInfo, and then when you open the next world, you take the score from UserInfo. More details about it can be found here: http://www.greenfoot.org/files/javadoc/greenfoot/UserInfo.html. There are also some good discussions on how to use UserInfo.
Is there a way to go level to level with the score using score class? The score can reset after each game.
Well, the UserInfo is an easy way, but if you don't want to use that, there's two other things I can think of. You could write a file that keeps track of your score, and you update it and use it between levels. Or you could make a class that keeps track of all your worlds in a array, and it has the score tracker that's used in each world (you would have to make a method or have the parameter of the worlds accept the score counter so they have it) And then when you teleport you would have to call a method from that class. I really think UserInfo would be the easiest/simplest way to do this. Especially if you're planning on uploading it to the Greenfoot site. It saves time, and there's less code for you involved. Note with the file technique, it would not work on the site, since applets can't save a file onto another's computer.
danpost danpost

2013/5/8

#
FlyingRabidUnicornPig wrote...
Well, the UserInfo is an easy way
Unfortunately, you would have to rely on the user being logged in for the UserInfo storage to be usable.
Yah, that too. Forgot about that.
danpost danpost

2013/5/8

#
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):
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.
SWAG SWAG

2013/5/8

#
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):
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.
Thank you for your help, but i'm a noob to Greenfoot. Could you explain the code and what should i put where (variables).
danpost danpost

2013/5/8

#
The code presumes that you have instance fields for the main Player object and the score Counter object either in each type of world or in the superclass of all the worlds those objects can be in. The fields would be something like this:
private Player player;
private Counter scoreCounter;
From the current world, you would create the new world and call the 'passObjects' method as follows:
World2 world2 = new World2();
world2.passObjects(player, scoreCounter);
Greenfoot.setWorld(world2);
With this, the objects are passed to the newly created world that is then set to be the current world. The 'passObjects' method sets its instance fields to the objects from the old world and adds them into itself. Unfortunately, you will probably determine that the new world it to be initiated from an actor class. This means that you will need to have a reference to the current world to get the counter and player objects; and that means you may have to determine which type world you are in (unless you superclass all the worlds that your player and counter can be in -- then you can immediately type cast the return of 'getWorld' to that class).
SWAG SWAG

2013/5/10

#
danpost wrote...
The code presumes that you have instance fields for the main Player object and the score Counter object either in each type of world or in the superclass of all the worlds those objects can be in. The fields would be something like this:
private Player player;
private Counter scoreCounter;
From the current world, you would create the new world and call the 'passObjects' method as follows:
World2 world2 = new World2();
world2.passObjects(player, scoreCounter);
Greenfoot.setWorld(world2);
With this, the objects are passed to the newly created world that is then set to be the current world. The 'passObjects' method sets its instance fields to the objects from the old world and adds them into itself. Unfortunately, you will probably determine that the new world it to be initiated from an actor class. This means that you will need to have a reference to the current world to get the counter and player objects; and that means you may have to determine which type world you are in (unless you superclass all the worlds that your player and counter can be in -- then you can immediately type cast the return of 'getWorld' to that class).
Thanks for your help, but I have a question I switch the worlds though my player class. Could you help me again.
danpost danpost

2013/5/10

#
Do you have a superclass for all your worlds that the player and counter can be in (other than the 'World' class)?
SWAG SWAG

2013/5/11

#
danpost wrote...
Do you have a superclass for all your worlds that the player and counter can be in (other than the 'World' class)?
NO I don't.
danpost danpost

2013/5/11

#
It would be best to create a super-class for those worlds that the player and counter can be in. That way you only have to put those needed fields and methods in one place; also, you will not have to determine which world is which, because you will have a constant world class name to cast references to in order to use the methods that you need to retrieve the values of those fields. You can also use a static field in this super-world to keep track of which sub-world is active (this will assist in determining which world to go to next).
SWAG SWAG

2013/5/11

#
danpost wrote...
It would be best to create a super-class for those worlds that the player and counter can be in. That way you only have to put those needed fields and methods in one place; also, you will not have to determine which world is which, because you will have a constant world class name to cast references to in order to use the methods that you need to retrieve the values of those fields. You can also use a static field in this super-world to keep track of which sub-world is active (this will assist in determining which world to go to next).
How would I create a super class?
danpost danpost

2013/5/11

#
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.
There are more replies on the next page.
1
2
3