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

2019/1/26

Variable Passing

1
2
danpost danpost

2019/2/12

#
Please post the code of your GameScene class for review.
northernlights176 northernlights176

2019/2/12

#
You meant the GameScene and not the TitleScreen right?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameScene here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameScene extends World
{

    public GreenfootSound bgMusic;
    /**
     * Constructor for objects of class GameScene.
     * 
     */
    public GameScene()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        bgMusic = new GreenfootSound("gameSceneMusic.mp3");
    }
    
    /**
     *  Method to play the background music in the game scenes
     */
    public void playSound()
    {
        bgMusic.playLoop();
    }// end method playSound
    
}
danpost danpost

2019/2/13

#
northernlights176 wrote...
You meant the GameScene and not the TitleScreen right? << Code Omitted >>
Yeah. I wanted to see what it does for its extensions. It is not a cause of your current concern; however, I do not really see a need for the class (it actually creates and plays multiple instances of the bg music -- one for each time an object is created for it or any of its extensions). Let us start afresh -- back to your originally posted code (w/o any of the changes I previously suggested) and think about this logically. You start with a TitleScreen object. Fine -- let that be your main place for storing your other worlds and your bridge between them. The ResultsScene field can remain null until such time as one is needed. Its non-null state can be used to determine when a new scenario is to begin after the initial start. You would, in brief, start with this:
import greenfoot.*;

public class TitleScreen extends World
{
    static TitleScreen titleScreen;
    
    GreenfootSound bgMusic = new GreenfootSound("gameSceneMusic.mp3");
    ResultsScene resultsScene;
    BariatricSurgery bariatricScene;
    InsanityPlea insanityScene;
    
    public TitleScreen()
    {
        titleScreen = this;
        bariatricScene = new BariatricSurgery();
        insanityScene = new InsanityPlea();
    }
    
    public void showResults()
    {
        resultsScene = new ResultsScene();
        Greenfoot.setWorld(resultsScene);
        bgMusic.stop();
        Greenfoot.stop();
    }
}
Of course, you will have more in the class. What I have shown, however, is a main class containing references to all your worlds, accessible to all those worlds. No world should be created or referenced (fielded) outside this class. You should have minimum difficulty building everything else around this. Btw, there will be no need for static content in the other classes.
You need to login to post a reply.
1
2