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

2013/10/22

Making an actor carry over to another world

winsticknova winsticknova

2013/10/22

#
I'm making a game and I have a score counter. When you lose the game it takes you to a game over screen. I want to display the final score on the game over screen but I can't get it to work. help please!
public int getScore()
    {
        return scoreboard.getScore();
    }
thats my code to display the code and i have
        scoreboard = new ScoreBoard();
        addObject(scoreboard, 79, 19);
in my constructor. i put that same code in my game over screen world. but it just creates a new scoreboard with zero, instead of carrying the old score over.
danpost danpost

2013/10/22

#
At least you already understand that the 'new ScoreBoard' is a new scoreboard and is different that the one you already had (hence, it is initialized with a score of zero). You can remove that line from the game over screen world. Here are a couple ways to get the final score across to the game over world. One is passing the final score itself or the scoreboard object as an argument in the constructor call (or you could even pass the world object itself). One is, after creating the game over world, call a method in it that receives the final score or the scoreboard object and updates the display. If your game over world had the name 'GameOver' and the world with the working scoreboard was called 'MyWorld', then passing the final score could be accomplished with the following from an Actor class:
// creating the new game over world
GameOver go = new GameOver(((MyWorld)getWorld()).getScore());
// GameOver world constructor
public GameOver(int score)
{
    int finalScore = score;
    // create display
    Greenfoot.stop();
}
winsticknova winsticknova

2013/10/22

#
so i would put that in one of my actors? or would i put it in my AvoiderGameOverScreen world (that's the name of my game over world). my constructor for my AvoiderGameOverScreen looks like this right now.
public AvoiderGameOverScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(600, 400, 1); 
        musics = new GreenfootSound("sounds/Scientist.mp3");
        scoreboard = new ScoreBoard();
        addObject(scoreboard,79,19);
    }
danpost danpost

2013/10/22

#
What type of class are you creating the AvoiderGameOverScreen from (what class is 'new AvoiderGameOverScreen' in)? an Actor or World subclass?
winsticknova winsticknova

2013/10/22

#
my AvoiderGameOverScreen is a subclass of World. and my scoreboard is a subclass of Actor.
danpost danpost

2013/10/22

#
Yes, but where (in which class) did you put the code 'new AvoiderGameOverScreen()'?
winsticknova winsticknova

2013/10/22

#
my endGame method:
public void endGame()
    {
        music.stop();
        AvoiderGameOverScreen go = new AvoiderGameOverScreen();
        Greenfoot.setWorld(new AvoiderGameOverScreen());
        
    }
is in my original world where i play the game: AvoiderWorld. but its called in my Avatar class ( the actor that I am able to control):
public void remove()
    {
        Actor avatar = getOneIntersectingObject(Enemy.class);
        Actor avatarr = getOneIntersectingObject(Enemyy.class);
        if (avatar != null)
        {
            AvoiderWorld world = (AvoiderWorld) getWorld();
            world.removeObject(this);
            world.endGame();
        }
        if (avatarr != null)
        {
            AvoiderWorld world = (AvoiderWorld) getWorld();
            world.removeObject(this);
            world.endGame();
        }
    }
danpost danpost

2013/10/22

#
Change your 'endGame' method to this:
public void endgame()
{
    music.stop();
    AvoiderGameOverScreen go = new AvoiderGameOverScreen(scoreboard);
    Greenfoot.setWorld(go);
}
and change your AvoiderGameOverScreen constructor to this:
public AvoiderGameOverScreen(ScoreBoard scoreboard)
{
    super(600, 400, 1);
    musics = new GreenfootSound("sounds/Scientist.mp3");
    addObject(scoreboard, 79, 19);
}
winsticknova winsticknova

2013/10/22

#
wow!! it works! thank you so much! been working on this forever. you are awesome
You need to login to post a reply.