This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
OrangeFlash81
OrangeFlash81 wrote ...

2012/8/24

How do I print my score with this script?

OrangeFlash81 OrangeFlash81

2012/8/24

Heya, I'm having a little bit of a problem getting my score from the last game to print. Basically, I have an actor called 'Scoreboard' and when I load a world called MyWorld it prints the score up at the top (successfully). I have another world called GameOver which is switched to when Rocket.class hits Asteroid.class. I want to print the same score I got from the last game on GameOver, but when I try it just says the score is 0. Here's the code for the scoreboard that you may need:
import greenfoot.*;
import java.awt.Color;

public class Scoreboard extends Actor
{
    int score = 0;
    
    public Scoreboard()
    {
        updateBoard(); //Tells the board to update when called
    }
    
    private void updateBoard()
    {
        setImage(new GreenfootImage("Orbs: " + score, 20, Color.black, new Color(0, 0, 0, 0))); //Print text
    }
    
    public void add(int addVal)
    {
        score += addVal; updateBoard(); //Calculate the score
    }
    
    public int getScore()
    {
        return score;
    }
}
Thanks, OF
erdelf erdelf

2012/8/24

sure, it prints 0 cause you set it to zero on the beginning. You need access to the integer in the other world, my game arena should help you, but in short, you have many way to do this, save the score in a parent world or ask in the constructor or make the int in the game world static
danpost danpost

2012/8/24

The easiest way is to pass the score to the GameOver world in a parameter. The constructor of the world should be:
public GameOver(int score)
The 'score' is then added to the Scoreboard object after it is created, if you are using the Scoreboard class to create the score display in this class. Either way, 'score' will hold the appropriate value. Set the new GameOver world with:
Greenfoot.setWorld(new GameOver(scoreboard.getScore()));
This is assuming you have a reference to the scoreboard and you named it 'scoreboard'. If you named it something else, just replace with that name.
OrangeFlash81 OrangeFlash81

2012/8/25

danpost wrote...
The easiest way is to pass the score to the GameOver world in a parameter. The constructor of the world should be:
public GameOver(int score)
The 'score' is then added to the Scoreboard object after it is created, if you are using the Scoreboard class to create the score display in this class. Either way, 'score' will hold the appropriate value. Set the new GameOver world with:
Greenfoot.setWorld(new GameOver(scoreboard.getScore()));
This is assuming you have a reference to the scoreboard and you named it 'scoreboard'. If you named it something else, just replace with that name.
OK, I'm now having quite a lot of problems. I have done the
public GameOver(int score)
thing in the GameOver world's code and it throws 'missing method body, or declare abstract'. Here's the code for the GameOver world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends World


{
    public Scoreboard scoreboard = new Scoreboard();
    public GameOver(int score); //<-- Here's the error
    {    
        super(600, 400, 1);
        GameOverText gameovertext = new GameOverText();
        addObject(gameovertext, 300, 100);
        Back back = new Back();
        addObject(back, 307, 360);
        PlayAgain playagain = new PlayAgain();
        addObject(playagain, 307, 200);
        GUIScore guiscore = new GUIScore();
        addObject(guiscore, 20, 10);
        addObject(scoreboard, 29, 15);
    }
}
And I also made the setWorld command different as you said but now the Rocket throws 'constructor GameOver in class GameOver cannot be applied to the given types; required: no arguments; found: int; reason: actual and formal argument lists differ in length' Here's all of the rocket's code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Rocket here.
 * This is the basic player class.
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Scoreboard scoreboard = new Scoreboard();  
    GreenfootSound backgroundMusic = new GreenfootSound("bgm.mp3");
    public void act() 
    {
        moveAndThrust();
        damage();
        pointget();
        uberorbget();
    }   
    public void moveAndThrust()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move(6);
            
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move(6);
           
        }
        if (Greenfoot.isKeyDown("left"))
        {
           setRotation(180);
           move(6);
           
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(6);
           
        }
    }
    public void damage()
    {
        Actor Asteroid;
        Asteroid = getOneObjectAtOffset(0,0, Asteroid.class);
        if (Asteroid !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(Asteroid);
            backgroundMusic.setVolume(0);
            Greenfoot.playSound("crash.wav");
            Greenfoot.setWorld(new GameOver(scoreboard.getScore())); //ERROR
        }
    }
    public void pointget()
    {
        Actor Point;
        Point = getOneObjectAtOffset(0,0, Point.class);
        if (Point !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(Point);
            MyWorld myworld = (MyWorld) getWorld();
            Scoreboard sBoard = myworld.scoreboard;
            sBoard.add(1); // adds 1 to the score
        }
    }
    public void uberorbget()
    {
        Actor UberOrb;
        UberOrb = getOneObjectAtOffset(0,0, UberOrb.class);
        if (UberOrb !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(UberOrb);
            MyWorld myworld = (MyWorld) getWorld();
            Scoreboard sBoard = myworld.scoreboard;
            sBoard.add(10); // adds 10 to the score
        }
    }
    

    
            
}
Thanks, OF
danpost danpost

2012/8/25

The main problem (why your score keeps coming up zero) is because of the 'GameOver' world code, specificly lines 13 and 25. You are creating a NEW scoreboard (one that nothing has been added to) and displaying that score. An easy fix at this point is to insert a line at 25 (just before adding the scoreboard object to the world)
scoreboard.add(score);
The reason you are getting an error on 'public GameOver(int score);' is because you have a semi-colon ( ; ) at the end of that line which should NOT be there. Remove it. Line 14 in the Rocket actor code should be removed (your scoreboard is in the MyWorld world code). Finally, line 61 should be replaced with the following:
MyWorld myworld = (MyWorld) getWorld();
Scoreboard sBoard = myworld.scoreboard;
Greenfoot.setWorld(new GameOver(sBoard.getScore()));
You should not longer get any errors and your scoreboard should work. Again, if any problems, post back.
You need to login to post a reply