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

2014/3/2

Passing score

PZ2010 PZ2010

2014/3/2

#
Hi, i am trying to do a multiple level game,but the score keeps going back to 0 when i enter the next level. Here is my code in player class:
private int score=0;

public void addScore()  
    {  
        if (!getWorld().getObjects(Scoreboard.class).isEmpty())  
        {  
            ((Scoreboard) getWorld().getObjects(Scoreboard.class).get(0)).changeScore();  
        }  
    }
and here is what i did in the scoreboard class:
private int score=0;

public void changeScore() 
    {
        score=score+100;
        GreenfootImage image=getImage();
        image.clear();
        image.drawString("Score:" + score,5,25);
    }    
Can someone help me? please
danpost danpost

2014/3/2

#
The displayed score value is from the score field in the scoreboard class. I do not see any reason to have a score value in the player class. It appears (by line 7 of the player class code above) that you do not have a reference to the scoreboard object in the player class (which is all right, since you obviously only have one scoreboard object in the world (if any?) -- I am just making note of it). When you call into being the next level world, get a reference (like in line 7 in the player class) to the counters in both worlds and then set the new world counter score value to that of the current one. The code would be something like this:
Level2 level = new Level2();
newCounter = (Counter)level.getObjects(Counter.class).get(0);
oldCounter = (Counter0getWorld().getObjects(Counter.class).get(0);
newCounter.setValue(oldCounter.getValue());
Greenfoot.setWorld(level);
I am presuming that you have 'get' and 'set' methods for the score in the scoreboard class.
PZ2010 PZ2010

2014/3/2

#
I understand your point, but are "newCounter and oldCounter"? Are those suppose to be two variables I need to create in both worlds?
danpost danpost

2014/3/2

#
PZ2010 wrote...
I understand your point, but are "newCounter and oldCounter"? Are those suppose to be two variables I need to create in both worlds?
No. Sorry. You can create them local.
Level2 level = new Level2();
Counter newCounter = (Counter)level.getObjects(Counter.class).get(0);
Counter oldCounter = (Counter0getWorld().getObjects(Counter.class).get(0);
newCounter.setValue(oldCounter.getValue());
Greenfoot.setWorld(level);
PZ2010 PZ2010

2014/3/2

#
So, i should put this code into my scoreboard class? I am a little confused, here is my whole code for scoreboard:
public class Scoreboard extends Actor
{
    private int score=0;
    private String text;
    public Scoreboard()
    {
        GreenfootImage image=new GreenfootImage(150,25);
        image.setFont(new Font("Bookman Old Style",Font.BOLD,24));
        image.drawString("Score:" + score,5,20);
        setImage(image);
    }
    
    /**
     * Changes the score of the scoreboard.
     */
    public void changeScore() 
    {
        score=score+100;
        GreenfootImage image=getImage();
        image.clear();
        image.drawString("Score:" + score,5,25);
    }    
    
    public int getSocre()
    {
        return score;
    }
}
and here is my code for player:
public void addScore()  
    {  
        if (!getWorld().getObjects(Scoreboard.class).isEmpty())  
        {  
            ((Scoreboard) getWorld().getObjects(Scoreboard.class).get(0)).changeScore();  
        }  
    }

/**
     * Turns to the next level if the player reached required score.
     */
    public void nextLevel()
    {
        if(score==600) 
        {
            nextLevel1();
        }

        if(score==1000) 
        {
            nextLevel2();
        }

        if(score==1600) 
        {
            nextLevel3();
        }
    }

    /**
     * Turns to level two if the player reached 600 points.
     */
    public void nextLevel1()
    {
        Maze theWorld = (Maze)getWorld();
        theWorld.stopSound();
        Greenfoot.playSound("win1.mp3");
        Greenfoot.delay(60);
        Greenfoot.setWorld(new Maze2()); 
    }

    /**
     * Turns to level three if the player reached 1000 points.
     */
    public void nextLevel2()
    {
        Maze2 theWorld = (Maze2)getWorld();
        theWorld.stopSound();
        Greenfoot.playSound("win2.mp3");
        Greenfoot.delay(50);
        Greenfoot.setWorld(new Maze3()); 
    }

    /**
     * Turns to ending scene if the player reached 1600 score.
     */
    public void nextLevel3()
    {
        Maze3 theWorld = (Maze3)getWorld();
        theWorld.stopSound();
        Greenfoot.delay(10);
        Greenfoot.setWorld(new Ending()); 
    }
and I put nothing related in my maze1,2,3(level1,2,3) class
danpost danpost

2014/3/2

#
I think what is causing the confusion is the fact that you have TWO fields to hold the same score (or at least hopefully -- since one field is in the class of the player and the other is in the class of the scoreboard). You are using the score field in the player class to determine when to change worlds and the 'addScore' method in that class is changing the value of the score field in the scoreboard class. These two fields are not linked in any way and one will not change if the other one does. It would be best to remove one of the two score fields and use the one remaining for all related purposes. Although I prefer to place the score field in either the world or player class and use a basic Text actor to display the value (and since you already have the scoreboard class), you can remove the one from the player class. Either way, it would help to have a field to hold the scoreboard object itself in the player class.
// add instance field in player class
private Scoreboard scoreboard; // remove 'private int score;'
// and to make things easy, add this method to the player class
public void addedToWorld(World world)
{
    scoreboard = (Scoreboard)world.getObjects(Scoreboard.class).get(0);
}
This method will set the Scoreboard object to the scoreboard field added above Make sure that the Scoreboard object is added to all your worlds before the player is added. Now, to 'addScore', you just use 'scoreboard.changeScore();'. To check changing levels, use 'if (scoreboard.getScore() ==600)' (as an example). Make sure that the method declaration statement is spelt correctly 'public int getScore()'. You can add the 'setScore' method I described above and use it in your world constructors (or prepare methods) as follows:
Scoreboard scoreboard = new Scoreboard();
scoreboard.setScore(600); // for Maze2 world
addObject(scoreboard, /* x, y */);
PZ2010 PZ2010

2014/3/3

#
Thank you so much, but I am not sure how to do the "setScore" method, since I don't have a "setValue" method originally
danpost danpost

2014/3/3

#
You then need to check out the Java trail called Learning the Java language. The tutorial that will most help at this time is called Classes and Objects; particularly the section called Classes.
You need to login to post a reply.