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

2021/4/5

Carrying a variable value over worlds?

CreatorMoon CreatorMoon

2021/4/5

#
I'm having trouble carrying a variable ("score" in this instance) across multiple worlds. What I'm aiming for is my game's "game over" screen to display the score, however I'm struggling with it. I'm pretty sure I took part in a discussion a few years ago that aimed to solve the issue, but I can't find it again. Does anyone know what I could do? My Code: Variable Holder Class/ScoreCounter:
import greenfoot.*;
public class ScoreCounter extends Actor
{
    int score = 0; //The variable I want to carry.
    public void act() 
    {
        setImage(new GreenfootImage("Score: "+score,25,Color.WHITE,Color.BLACK));
    }
}
Game Over Screen/LoseScreen (I want to carry over the variable value to this world):
import greenfoot.*;
public class LoseScreen extends World
{
    public LoseScreen()
    {
        super(496, 500, 1);
        ScoreCounter scorecounter = getObjects(ScoreCounter.class).get(0);
        showText("Score: "+scorecounter.score, 248, 250);
    }
}
Any help would be appreciated, thanks!
CreatorMoon CreatorMoon

2021/4/5

#
I almost forgot, I keep getting this error message with my current code:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.get(ArrayList.java:458)
	at LoseScreen.<init>(LoseScreen.java:7)
	at Ship.act(Ship.java:20)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
Here's my Ship actor, by the way:
import greenfoot.*;
public class Ship extends Actor
{
    private int blastDelay = 39;
    public void act()
    {
        ScoreCounter scorecounter = getWorld().getObjects(ScoreCounter.class).get(0);
        movement();
        blastingcheck();
        if (isTouching(AlienBullet.class)||isTouching(Alien1.class))
        {
            Greenfoot.playSound("destroy.mp3");
            removeTouching(AlienBullet.class);
            removeTouching(Alien1.class);
            LivesCounter livescounter = getWorld().getObjects(LivesCounter.class).get(0);
            livescounter.lives = livescounter.lives - 1;
            setLocation(250,450);
            if(livescounter.lives == 0)
            {
                Greenfoot.setWorld(new LoseScreen());
            }
        }
        if(scorecounter.score == 945)
        {
          Greenfoot.setWorld(new WinScreen());
        }
    }
    public void blastingcheck()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            blastDelay++;
            if(blastDelay == 40)
            {
                blast();
                blastDelay = 0;
            }
        }
    }
    public void movement()
    {
        if (Greenfoot .isKeyDown("a"))
        {
            move(-5);
        }
        if (Greenfoot .isKeyDown("d"))
        {
            move(5);
        }
    }
    public void blast()
    {
        getWorld().addObject(new PlayerBullet(), getX(), getY()-1);
        Greenfoot.playSound("shoot.mp3");
    }
}
rocket770 rocket770

2021/4/5

#
I could be wrong but if you made the score a public static variable you should just be able to reference it with int score = ScoreCounter.score in your ship actor. This means, that in the constructor of your ScoreCounter class, you would need to set the score back to zero. Every ScoreCounter object will share this value. The other way to go about it which would just be easier if you do use multiple ScoreCounter objects would be referencing the score counting object, grab the score variable and pass it through the constructor of the new world.
Super_Hippo Super_Hippo

2021/4/5

#
ScoreCounter scorecounter = getObjects(ScoreCounter.class).get(0);
This line is giving you the error because there is no ScoreCounter object in the world you just created. If the only purpose of the LoseScreen is to display the score, you might just pass the value of the variable.
import greenfoot.*;
public class LoseScreen extends World
{
    public LoseScreen(int score)
    {
        super(496, 500, 1);
        showText("Score: "+score, 248, 250);
    }
}
if(livescounter.lives == 0)
{
    ScoreCounter scorecounter = getObjects(ScoreCounter.class).get(0);
    Greenfoot.setWorld(new LoseScreen(scorecounter.score));
}
CreatorMoon CreatorMoon

2021/4/7

#
I've got it working. Thanks for the help guys!
You need to login to post a reply.