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
trias95 trias95

2014/3/30

#
So im approaching the last stages of my game. everything works now except i seem to be unable to call the score stored in a counter class and display it on the game over world screen despite being able to display it on the in game world screen. My guess is that my problem is in here somewhere
public class Zombie extends CHARACTERS
{
    private int score = 0;
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    
    {
        followHuman();
        move(1);

        Actor Survivor;
        Survivor = getOneObjectAtOffset(0,0, Survivor.class);
        if (Survivor != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Survivor);
            world = getWorld();
            
            Background background = (Background)world;
           Counter counter = background.getCounter();
           score = counter.getScore(score);
           world.removeObject(this);
           
            Greenfoot.setWorld(new GameOver(score));

        }  

    }
this is a part of my zombie code and as you can see, when it reaches a survivor class it is supposed to activate the game over screen (which it does) however the game over screen counter will always display 0 and i cant understand why when it works fine "in game" any suggestions? any other areas of code i can show you guys to assist you in assisting me?
trias95 trias95

2014/3/30

#
Getting a little further on my own. ive narrowed it down to something wrong in this section of code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends OBJECTS
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage(" " + score, 24, Color.WHITE, Color.BLACK));
        if (GameOver.class.isInstance(getWorld()))
        {
            score = getScore(score);
            setImage(new GreenfootImage(" " + score, 24, Color.WHITE, Color.BLACK));
        }
    }    
    public void addScore()
    {
        score = score + 50;
    }
    public void addScore2()
    {
        score = score + 25;
    }
    public int getScore(int Score)
    {
            
           return *********;
    }
    
}
If i write a number where the *'s are then on the game over screen it displays that number. i need it to return the score but whenever i try just typing in "score" it says its 0 :s
danpost danpost

2014/3/30

#
Well, one thing is that you are passing a value to the 'getScore' method of the Counter class when you are actually trying to get a value from it. Change the 'getScore' method in the Counter class to this:
public int getScore()
{
    return score;
}
Remove line 22 from the Counter class ('score' is readily available and you do not have to 'get' it in this class). Then in the Zombie class, remove line 3 (this is probably what is causing the problem) and change line 25 to:
int score = counter.getScore();
trias95 trias95

2014/3/30

#
ive made the changes you have suggested and i can see and understand why they had to be made however the score is still not showing on the game over screen. the code to display the counter in the game over screen is exactly the same as the one on the game screen so i'm really puzzled as to why it will work for one and not the other :/ The counter does display, it does become a 0 as opposed to the greenfoot foot image but it doesnt display the actual score. Below i'll attach both my working background (in game) code and the game over screen code.
public class Background extends World
{
    static GreenfootSound music = null;  

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

        prepare();

        

        Start.title.stop();

        /**
         * Prepare the world for the start of the program. That is: create the initial
         * objects and add them to the world.
         */
    }
    
    public void act()
    
    {
         if( !music.isPlaying())
        {
            music.setVolume(50);
            music.playLoop();
        }
    }

    public Counter getCounter()
    {
        return counter;
    }

    private void prepare()
    {
        addObject (counter, 335,11);
        Survivor survivor = new Survivor();
        addObject(survivor, 391, 323);
        Spawner spawner = new Spawner();
        addObject(spawner, 89, 311);
        scoreboard scoreboard = new scoreboard();
        addObject(scoreboard, 715, 535);
        Start.title.stop();  
    }
}
game over code:
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
{
    public int score = 0;
    static GreenfootSound laugh = null;  

    static {  
        laugh = new GreenfootSound("gameOverLaugh.mp3");  
    }
    Counter counter = new Counter();
    
    /**
     * Constructor for objects of class GameOver.
     * 
     */
    public GameOver()
    {    
        // 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();

        

        
    }
    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);
        Counter counter = new Counter();
        addObject(counter, 462, 575);
        counter.setLocation(454, 568);
    }
}
As you can see, there isnt really a difference in the way these two worlds call the counter so im not sure why one works and the other doesnt :/
danpost danpost

2014/3/30

#
The counter in one world is similar to the counter in the other world; but, they are definitely not 'the same' counter. They are the same type of Counter object; but, two separate objects with separate fields with separate values. You can, instead of passing the score value, pass the Counter object itself and have the GameOver world add it to itself. Change line 24 of the GameOver world to 'public GameOver(Counter counter)'. Remove line 66 and line 12 from the GameOver world. Change line 18 of the GameOver world to 'Counter counter = null;'. Add at line 37 in the GameOver world 'this.counter = counter;'. In your Zombie class (from above) remove line 25 ( ' int score = counter.getScore();' ) Change line 28 in the Zombie class (from above) to 'Greenfoot.setWorld(new GameOver(counter));'.
trias95 trias95

2014/3/30

#
Ive made the relevant changes however now when i am eaten by a zombier, instead of passing me to the game over world it now simply stops the game and leaves me on the game screen? any ideas?
danpost danpost

2014/3/30

#
Please show the Zombie class, as it is now. May ask for more code if I do not see anything there.
trias95 trias95

2014/3/30

#
import greenfoot.*;
import java.util.List;    // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends CHARACTERS
{
    
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    
    {
        followHuman();
        move(1);

        Actor Survivor;
        Survivor = getOneObjectAtOffset(0,0, Survivor.class);
        if (Survivor != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Survivor);
            world = getWorld();
            
            Background background = (Background)world;
           Counter counter = background.getCounter();
           
           world.removeObject(this);
           
            Greenfoot.setWorld(new GameOver(counter));

        }  
        Actor barrels;
        barrels = getOneObjectAtOffset(0,0, Barrels.class);
        if (barrels != null)
        {
            setRotation(getRotation()-180);
            move(5);
        }
    }

       
    public void followHuman()  
    {  
        double dist = 800;  
        Actor closest = null;  
        if(!getObjectsInRange(800, Survivor.class).isEmpty())  
        {  
            for (Object obj: getObjectsInRange(800, Survivor.class))  
            {  
                Actor guy = (Actor) obj;  
                double guyDist = Math.hypot(guy.getX() - getX(), guy.getY() - getY());  
                if (closest == null || guyDist< dist)  
                {  
                    closest = guy;  
                    dist = guyDist;  
                }  
            }  
            turnTowards(closest.getX(),closest.getY());  
        }     
    }  
}
danpost danpost

2014/3/30

#
It is probably because you are getting an error before it has a chance to change worlds. Move lines 40 through 46 of the Zombie class and insert it at line 22.
trias95 trias95

2014/3/30

#
No the error still exists. it still refuses to change to the other world :/
danpost danpost

2014/3/30

#
trias95 wrote...
No the error still exists. it still refuses to change to the other world :/
Anytime you get an error, please show the error message in its entirety and show the relevant code.
trias95 trias95

2014/3/30

#
I think i've phrased that wrong. i mean the error of it not changing worlds. there is no error code for it, nothing in the terminal or anything it simply stops running when it is supposed to change screen
danpost danpost

2014/3/30

#
The only thing I can think of that would do that is that you must be using a 'Greenfoot.stop();' statement somewhere. Or, are your 'Run', 'Act', and 'Reset' button inactive when it stops?
trias95 trias95

2014/3/30

#
No they do not become inactive. it ends and i can click run again and the zombies move but without the survivor on the screen. I will have a look around for the stop method however this is the only way for the gam,e to end and it only happens when the screen is set to change so i would assume if there was one, it would be in the zombie code
danpost danpost

2014/3/30

#
The only other thing that "maybe" causing this (and I kind of doubt it, but) is the naming of your local variable Survivor, which is exactly the name of the class. Use lowercase letters to begin field and variable names and uppercase letters to begin class names; and try not to use the same name for more than one thing (a change in case makes it different).
There are more replies on the next page.
1
2
3
4