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?

1
2
3
4
5
trias95 trias95

2014/3/30

#
I have a feeling this is something to do with it :/
java.lang.NullPointerException at greenfoot.World.addObject(World.java:392) at GameOver.prepare(GameOver.java:67) at GameOver.<init>(GameOver.java:29) at Zombie.act(Zombie.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/3/30

#
Ok. What do you now have for your GameOver class code? BTW, that is an error message.
trias95 trias95

2014/3/30

#
Game over is a world. and yeah i know it is, sorry i didnt see that earlier -__-
import greenfoot.*; 
import java.awt.Color;

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends World
{
    
    static GreenfootSound laugh = null;  

    static {  
        laugh = new GreenfootSound("gameOverLaugh.mp3");  
    }
    Counter counter = null;
    
    /**
     * Constructor for objects of class GameOver.
     * 
     */
    public GameOver(Counter counter)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 

        prepare();

        

        Background.music.stop();
        Survivor.machinegun.stop();
        turretBase.turret.stop();

        this.counter = counter;

        
    }
    public void act() 
    {
        if( !laugh.isPlaying())
        {
            laugh.setVolume(50);
            laugh.playLoop();
        }

    }    
    
    public Counter getCounter()
    {
        return counter;
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    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);
    }
}
That is the entire game over world code
danpost danpost

2014/3/30

#
Alright. We will have to back-track. Insert the following at line 31 of the GameOver class:
System.out.println("Counter object is null: "+(counter == null));
and report back what is put to the terminal.
trias95 trias95

2014/3/30

#
right. so when i now die, it still srashes and nothing other than that null pointer exception i pointed out earlier. when trying to load the game over world via click on it and selecting new GameOver (Counter counter) it gives me this window
trias95 trias95

2014/3/30

#
image link here, not sure why it didnt work - _ - create object window
danpost danpost

2014/3/30

#
Because your GameOver world requires a Counter object to be created with, greenfoot is asking for the Counter object to create the new GameOver object with. You can right click on your Counter object in the Background world and select 'Inspect'; then right click on the GameOver Icon to load the GameOver world and type in 'counter' (without the quotes) and select OK. However, doing this does not ensure that the correct counter or one at all is being passed to the new GameOver object when running.
danpost danpost

2014/3/30

#
danpost wrote...
Alright. We will have to back-track. Insert the following at line 31 of the GameOver class:
System.out.println("Counter object is null: "+(counter == null));
and report back what is put to the terminal.
You should try this.
trias95 trias95

2014/3/30

#
i see. is it possible then to just pass an integer over representing what the counter is on the game world and then having a separate counter in the game over world turn that int into a display of a score?
danpost danpost

2014/3/30

#
That is possible; but, not with the Counter code that you currently have. But, even so, if the Counter object we are passing is null, then there is no score to get because it does not exist from where we are trying to get it from..
trias95 trias95

2014/3/30

#
Okay. I think what i need to do then is scrap the counter for a moment. get it so that the game over screen displays as it did and then work on an alternative scoring method
trias95 trias95

2014/3/30

#
I'd just like to say thank you very much man, for all your help on this thread and other. Ive not been using greenfoot for very long but with a lot of help ive almost finished creating something im quite proud of. So yeah, Thanks very much for your continued support and sorry if i've asked some stupid questions or had some obviously wrong code in there somewhere D:
danpost danpost

2014/3/30

#
I think we can lick this now if you just bear with me.
danpost wrote...
danpost wrote...
Alright. We will have to back-track. Insert the following at line 31 of the GameOver class:
System.out.println("Counter object is null: "+(counter == null));
and report back what is put to the terminal.
You should try this.
I have asked two times now that you try this. It will probably take just a few steps to figure out what is happening.
trias95 trias95

2014/3/30

#
thats the problem. i have put that code in but im unable to run it as i tried to explain earlier. to run the game over world without dying i need to run it as the first world via the "new GameOver(Counter counter) command when right clicking on the world but doing that only shows me the image. and when typing in counter as you suggested it says "Error: cannot find symbol - variable counter"
danpost danpost

2014/3/30

#
But you need to die. The error is occurring while the Background world is still active.
There are more replies on the next page.
1
2
3
4
5