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

2014/3/30

Passing a score from one world to another?

2
3
4
5
6
trias95 trias95

2014/3/30

#
i understand :) just a moment ill remove all the sound now.
trias95 trias95

2014/3/30

#
http://www.greenfoot.org/scenarios/11183
trias95 trias95

2014/3/30

#
YES YES YES YES YES YES YES YES YES. ive fixed it! i added this to the counter
static int score = 0;  

    static {  
        score = 0;
    }  
to make sure that even when a new counter was created, the score stayed the same. in the game over world, this was added within the "Prepare" method
 Counter counter = new Counter();
        addObject(counter, 462, 575);
       counter.setLocation(454, 568);
Other changes were changing
public GameOver(Counter counter)
to
public GameOver()
and making the relevant changes in the zombie class so as to not try and return something..
Greenfoot.setWorld(new GameOver());
as opposed to:
[code]Greenfoot.setWorld(new GameOver(counter));
Thank you very much for your continued help and i hope that this can help someone else too in the future :) i don't think i've ever loved the word static so much in my life.
danpost danpost

2014/3/30

#
That way of fixing it is really a 'jury-rigging' type fix (or a 'cheat'). If you ever create another counter, you will not be able to keep the scores separate. But. whatever.
danpost danpost

2014/3/30

#
I downloaded your project and adjusted your GameOver world code to the following:
import greenfoot.*; 
import java.awt.Color;

public class GameOver extends World
{
    static GreenfootSound laugh = new GreenfootSound("gameOverLaugh.mp3");  

    Counter counter = null;
    
    public GameOver(Counter counter)
    {
        super(800, 600, 1); 
        this.counter = counter;
        prepare();
    }

    public void act() 
    {
        if( !laugh.isPlaying())
        {
            laugh.setVolume(50);
            laugh.playLoop();
        }
    }    
    
    private void prepare()
    {
        TryAgain tryagain = new TryAgain();
        addObject(tryagain, 681, 551);
        laughingSkull laughingskull = new laughingSkull();
        addObject(laughingskull, 683, 119);
        addObject(counter, 462, 575);
        counter.setLocation(454, 568);
    }
}
which is pretty much what I was saying above. You just could not seem to do everything I was trying to tell you to do. I seems to work ok with the above. I think you were just having trouble getting the constructor right.
trias95 trias95

2014/3/31

#
So i decided this morning against cheating my way through this and to learn to do it properly and i decided to redo the game over code with the one you corrected and it counts fine however without the static, the score isnt retained on the game over screen and when using the static, if i dont resert the scenario,k the counter retains its score and counts up ontop of what it was when playing a second time aroud :( Any ideas on what might be causing this? its obvious that when the game over world places the counter it is creating a new one which obviously doesnt keep the score from the game but im just not sure about how to make it create a counter but not a new one, to instead just move the old one to its new location? i think you get the jist.
danpost danpost

2014/3/31

#
The GameOver world 'stole' the counter from the Background world. It must now be given back.
Background bg = new Background();
bg.setCounter(counter);
Greenfoot.setWorld(new Background());

// the setCounter method in Background
public void setCounter(Counter counter)
{
    this.counter = counter;
    addObject(counter, x, y);
}
Actually, there is a better way to do this, which I will post in a little while (time to eat).
trias95 trias95

2014/3/31

#
Look forward to seeing what it is, also, where abouts does the code above go, in the Background world?
danpost danpost

2014/3/31

#
OK, this is what I came up with:
public class Background extends World
{
    static GreenfootSound music;
    static
    { 
        music = new GreenfootSound("inGameMusic.mp3");
        music.setVolume(50);
    }  

    Counter counter;

    public Background()
    {
        this(new Counter());
        Start.title.stop();
    }

    public Background(Counter counter)
    {
        super(800, 600, 1); 
        this.counter = counter;
        prepare();
    }

    private void prepare()
    {
        addObject(counter, 335,11);
        addObject(new Survivor(), 391, 323);
        addObject(new Spawner(), 89, 311);
        addObject(new scoreboard(), 715, 535);
    }
    
    public void act()
    {
        if(!music.isPlaying()) music.playLoop();
    }

    public Counter getCounter()
    {
        return counter;
    }
}
With this Background class and the extra constructor that has a Counter argument, you can, from the 'GameOver' world, pass the counter back with
Greenfoot.setWorld(new Background(counter));
trias95 trias95

2014/3/31

#
ive added that to the Background world, as for the post before, the one starting:
danpost wrote...
The GameOver world 'stole' the counter from the Background world. It must now be given back.
as for the code in that post, where is that going i got that the set counter method,
public void setCounter(Counter counter)  
{  
    this.counter = counter;  
    addObject(counter, x, y);  
}  
goes in the Background world which is where ive put ti but as for the rest im not sure. i tried to place it on my "TryAgain" button which, as it stands, is what re-loads the game world after a death. so i put.
Background bg = new Background();  
in place of
[code] World world;
           world = getWorld();
           world.removeObject(this);
           Greenfoot.setWorld(new Background());
bg.setCounter(counter); Greenfoot.setWorld(new Background()); so my new version was
World world;
           world = getWorld();
           
           Background bg = new Background();  
           bg.setCounter(counter);  
           Greenfoot.setWorld(new Background()); 
           world.removeObject(this);
but that then says "cannot find symbol - variable counter" and highlights the word "counter" on the line
bg.setCounter(counter);  
danpost danpost

2014/3/31

#
The 'setWorld' command on line 6 should be:
Greenfoot.setWorld(bg);
Your TryAgain button class will need to acquire the counter from the GameOver world in which it resides; so, you WILL need to 'getCounter' method in the GameOver class and use in the class of the button:
World world;
world = getWorld();
GameOver go;
go = (GameOver)world;
Counter counter = go.getCounter();
which can be simplified to this:
Counter counter = ((GameOver)getWorld()).getCounter();
trias95 trias95

2014/3/31

#
Why does the try again button need to get the counter that is in the game over world? if im not mistaken, shouldn't it work in this order Play game > gain score > die >that score is displayed on game over screen > try again > new counter with no score is created ?
trias95 trias95

2014/3/31

#
Also, just going along with what you were saying, it seems that the GameOver world doesnt have a get counter method? :s
danpost danpost

2014/3/31

#
trias95 wrote...
im just not sure about how to make it create a counter but not a new one, to instead just move the old one to its new location? i think you get the jist.
Maybe there was a misunderstanding here. No. You do not need to pass the counter back to the Background world to start a new play. The reason that the score was being added on top of the old game score is because of the 'static' condition of the Counter field. 'static' fields retain their values (or objects) until the project is re-compiled (resetting does not clear the fields).
trias95 trias95

2014/3/31

#
Yeah i understand that it doesnt need to be passed back in order to play again i was just curious as to why you want the TryAgain class to acquire the counter as that class is simply a button to replay the game.
There are more replies on the next page.
2
3
4
5
6