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

2020/12/3

Problem with linking worlds via buttons

Conkorde Conkorde

2020/12/3

#
So I have this very strange problem with a button that should take me back to the main title screen of my game. In my SettingsScreen, if I have a button that takes me back to the TitleScreen, the world turns grey forever. After a while, it gives me a StackOverflow error with a whole bunch of red lines that slowed down my entire pc significantly. I tried to restart Greenfoot, but then it keeps saying "The world is being constructed...". What's even weirder is that when I change TitleScreen to Playground, the world where the game can be played, the code works flawlessly. Can anyone help me? The lines that indicate errors are: at Playground.<init>(Playground.java:36) at TitleScreen.<init>(TitleScreen.java:7) The following errors keep stacking: at SettingsScreen.prepare(SettingsScreen.java:38) at SettingsScreen.<init>(SettingsScreen.java:11) at TitleScreen.prepare(TitleScreen.java:39) at TitleScreen.<init>(TitleScreen.java:12) at SettingsScreen.prepare(SettingsScreen.java:38) And so forth... Here's World TitleScreen:
import greenfoot.*;

public class TitleScreen extends World
{
    
    GreenfootSound titleMusic = new GreenfootSound("TitleMusic.wav");
    Button startButton = new Button("PlayBtn.png", new Playground());
    public TitleScreen()
    {
        
        super(600, 600, 1); 
        prepare(); //error
    }
    
    public void act()
    {
        handleMusic();
    }

    
    public void handleMusic()
    {
        if (!titleMusic.isPlaying())
        {
            titleMusic.play();
        }
        if (Greenfoot.mouseClicked(startButton))
        {
            titleMusic.stop();
        }
    }
    
    
    private void prepare()
    {
        TitleLabel titleLabel = new TitleLabel();
        addObject(titleLabel,280,90);
        addObject(startButton, 300, 250);
        Button settingsButton = new Button("SettingsBtn.png", new SettingsScreen()); //error
        addObject(settingsButton, 300, 380);
        Button guideButton = new Button("ControlsBtn.png", null);
        addObject(guideButton, 300, 510);
    }
}
And World SettingsScreen:
import greenfoot.*;

public class SettingsScreen extends World
{
    ToggleButton musicButton = new ToggleButton(true);
    SettingsLabel musicText = new SettingsLabel("Music (on)");
    public SettingsScreen()
    {    
        super(600, 600, 1); 
        prepare(); //error
    }
    
    public void act()
    {
    }
    
    public void changeText(boolean state)
    {
        if (state == true)
        {
            SettingsLabel.textString = "Music (on)";
        }
        else
        {
            SettingsLabel.textString = "Music (off)";
        }
    }
    
    private void prepare()
    {
        addObject(musicButton, 300, 250);
        addObject(musicText, 220, 242);
        Button backBtn2 = new Button("BackBtn2.png", new TitleScreen()); //error, changing to Playground() makes code work
        addObject(backBtn2, 300, 510);
    }
}
danpost danpost

2020/12/3

#
Conkorde wrote...
I have this very strange problem
That is not a strange problem. You have created a chain of events that will not end. You have world A creating a world B (during its construction), which creates another world A (during its construction), which, in turn, creates another world B; and on and on. Instead of passing the worlds to the buttons, pass a different int value for each world type. Let the button, maybe via a switch statement, create the world to proceed to when it gets clicked.
Conkorde Conkorde

2020/12/3

#
Wow, thanks danpost! I have to complete this project for school tomorrow, so I was kinda worried you wouldn't respond. Thanks for saving my day!
You need to login to post a reply.