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

2013/6/27

Keep score in new world

1
2
davemib123 davemib123

2013/6/27

#
I have managed to increment the score in Level1_A, but how do I get it to increment from the last number in Level1_B? My scenario is located here: http://www.greenfoot.org/scenarios/8881
davemib123 davemib123

2013/6/27

#
sorted it :)
davemib123 davemib123

2013/6/28

#
Got a new problem on this. I created a super class for World which contains this:
public class Levels extends World
{
    protected static Counter scoreCounter;
    protected static Counter lifeCounter;
    /**
     * Constructor for objects of class Levels.
     * 
     */
    public Levels()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1110, 590, 1, false);
        scoreCounter = new Counter ("Score");
        lifeCounter = new Counter ("Life");
        lifeCounter.setValue(3);
    }
    
     public static Counter getScoreCounter()
    {
        return scoreCounter;
    }
    
    public Counter getLifeCounter()
    {
        return lifeCounter;
    }
}
Level 1_A has this at the start:
public class Level1_A extends Levels
{
    /**
     * Constructor for objects of class Level1_A.
     * 
     */
    public Level1_A()
    {    
        Greenfoot.setSpeed(50);
        worldColor();
        populateWorld();
    }

    public void populateWorld()
    {

        addObject(scoreCounter, 1035, 21);
        addObject(lifeCounter, 1027, 51);
Level1_A allows the score to be incremented, but when I progress to Level1_B. The score reverts to 0. I have this in Level1_B start:
    public Level1_B()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        Greenfoot.setSpeed(50);
        Greenfoot.start();
        worldColor();
        populateWorld();
    }
    
    public void populateWorld()
    {
        scoreCounter.setValue(Level1_A.scoreCounter.getValue());
        addObject(scoreCounter, 1035, 21);
        addObject(lifeCounter, 1027, 51);
Any chance of helping me out?
davmac davmac

2013/6/28

#
Your Level1_B presumably extends Levels, so the Level1_B() constructor contains an implicit call to the Levels() constructor, which resets the counters (lines 13 and 14). Because the counters are static they are shared by all the Levels subclasses, so this effectively wipes the counters for Level1_A as well, making Level1_B line 12 ineffective. Really, the best solution from a design standpoint is to not use static for the counters, and have the Level1_B constructor take the counters from the previous level as parameters. However, a simple fix would be to add a second constructor to Levels which doesn't reset the counters (have it take a boolean parameter for instance, which isn't even needed except to differentiate from the original constructor), and have the Level1_B constructor explicitly call this constructor - i.e. "super(true);".
Zamoht Zamoht

2013/6/28

#
It's because every time you create a world which is subclass from Levels it will call
public Levels()  
    {      
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.  
        super(1110, 590, 1, false);  
        scoreCounter = new Counter ("Score");  
        lifeCounter = new Counter ("Life");  
        lifeCounter.setValue(3);  
    }  
This means that lifeCounter will be set to a new counter with the value 3 and scoreCounter will be set to a new counter with the value 0. To fix this put an if statement around the three lines of code, which checks that the counters == null. So only create a new counter if the counter is == null. Then: scoreCounter.setValue(Level1_A.scoreCounter.getValue()); shouldn't be necessary. Everytime! Always to slow.. Well I agree with davmac. Though if you have a lot of levels I would find a whole new way to go around this. If you have like 20 levels are you going to create 20 classes for that? You should try to use one class and then create a setupLevel(int level) method which will setup a new level. If you can make this work stuff like scores and counters will get a whole lot easier too.
davemib123 davemib123

2013/6/28

#
Thanks for the suggestions davmac and zamoht. With regards to "setupLevel(int level)", I read one of your posts to another thread: http://www.greenfoot.org/topics/find/25833#post_25833, I get about 85% of the code you wrote, but what would the act method contain? As you wrote honey.class
davemib123 davemib123

2013/6/28

#
davmac wrote...
Really, the best solution from a design standpoint is to not use static for the counters, and have the Level1_B constructor take the counters from the previous level as parameters.
How would I go about doing the above?
Zamoht Zamoht

2013/6/28

#
Don't look at the act method. It worked for something else. What you have to look at is the setupLevel method. The only thing you have to do is to create the setup code inside and then put code somewhere to tell when to change level.
davmac davmac

2013/6/29

#
How would I go about doing the above?
First remove the "static" keyword from the declarations of the variables. Then add two parameters to the Level1_B constructor, each of type Counter. In the constructor, set the values of scoreCounter and lifeCounter to the values of these counters that were passed in as parameters. When you construct the Level1_B world, you'll need to pass the counters from the previous world in.
davemib123 davemib123

2013/6/29

#
Zamoht wrote...
Don't look at the act method. It worked for something else. What you have to look at is the setupLevel method. The only thing you have to do is to create the setup code inside and then put code somewhere to tell when to change level.
I've done and tested the method setupLevel, this works fine. When changing level I have this so far, but how do I tell it that when Mario reaches a certain position to move to the next level?
    public void act()  
    {  
        List Mario = getObjects(Mario.class);  

        if ()
        {  
            setupLevel(level+1);  
        }  
    }  
Zamoht Zamoht

2013/6/29

#
First of all I would put something in the setupLevel code to know where the goal was (this is not necessary if the goal is at the same place in every level). Then in the worlds act method you could write something like:
public void act()
    {
        List mario = getObjectsAt(goal.x, goal.y, Mario.class);
        if (!mario.isEmpty())
        {
            setupLevel(level+1);
        }
    }
Though this only checks for Mario at one point (if just one pixel of mario is at the goal it will change level). If you want bigger goals I would create a goal class and then check with something like this: if (!(Actor)(mario.get(0)).getOneIntersectingObject(Goal.class).isEmpty()) { //Mario is at goal } Haven't tested this line, but it should work.
davemib123 davemib123

2013/6/29

#
Zamoht I got it working with goal.x and goal.Y. Could you elaborate for the the bigger goals?
Zamoht Zamoht

2013/6/29

#
Yes, if you want bigger goals you can create a new class (let's call it Goal). Then the constructor of Goal would look like this:
public Goal(int width, int height)
{
     setImage(new GreenfootImage(width, height));
}
The width and the height is the size of the goal. The line I send before should check if mario is intersecting with the goal class. Please tell me if you want to know more.
davemib123 davemib123

2013/6/29

#
I created the goal class. The line you sent above:
if (!(Actor)(mario.get(0)).getOneIntersectingObject(Goal.class).isEmpty())
When I place into Act method of the world it gives me the error:
cannot find symbol - method getOneIntersectingObject(java.lang.Class<Goal>)
Zamoht Zamoht

2013/6/29

#
Okay I found out that getOneIntersectingObject is a protected method. So here is what you can do. First of all create a method in your Mario class:
    public boolean isAtGoal()
    {
        Goal goal = (Goal)getOneIntersectingObject(Goal.class);
        if (goal != null)
        {
            return true;
        }
        return false;
    }
Then put this in your world's act method:
    public void act()
    {
        List mario = getObjects(Mario.class);
        if (!mario.isEmpty())
        {
            if (((Mario)(mario.get(0))).isAtGoal())
            {
                //Mario is at the goal
            }
        }
    }
I have tested this so it should work.
There are more replies on the next page.
1
2