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

2021/11/4

How to save the current instance of a world to be able to pass into other world classes

Cs-studentX Cs-studentX

2021/11/4

#
I am making a maze game which is called MyWorld, and when the player gets to an orb it will bring them to a new world which will display a question and four option buttons. Each option button is connected to a response page which is also another world. On the response page there is button that I want to be able to bring the player back to where they left off on the maze board (MyWorld). So what I have right now is that I am trying to save the current instance of MyWorld and some how pass into each question and response page so the player can return back to where they left off. However the issue that I'm having right now is that the "world" variable that I am passing into other World class it says is null. So I was wondering how do I save the current instance of "MyWorld" so that I can pass it into other worlds? If you need to see some code to better understand what I'm trying to do let me know. I look forward to hear from you guys. Thanks!
danpost danpost

2021/11/4

#
Cs-studentX wrote...
If you need to see some code to better understand what I'm trying to do let me know.
It is best to show all codes tried pertaining to the attempts at passing the MyWorld world through the other worlds. However, you may consider looking at my World Changing Demo scenario for examples.
Cs-studentX Cs-studentX

2021/11/4

#
In my MyWorld class here is the method where I try to pass the instance of MyWorld into the first question. MyWorld Class:
public void goToQuestion(int questionNum){
        //MyWorld world = (MyWorld)getWorld();
        
        if(questionNum ==1){
            Greenfoot.setWorld(new Question1(this));
        }
        
        if(questionNum ==2){
            Greenfoot.setWorld(new Question2(this));
        }
    }
Question 1 Class:
public class Question1 extends World 
{
    World mazeWorld;
    public Question1(World world){
        super(800, 600, 1); 
        prepare();
        mazeWorld = world;
    }
    
    private void prepare(){
        questionOne questionOne = new questionOne();
        addObject(questionOne,406,247);
        questionOne.setLocation(517,399);
        
        allNighterButton allNighterButton = new allNighterButton(mazeWorld);
        twoAllNightersButton twoAllNightersButton = new twoAllNightersButton(mazeWorld);
        emailMirskyButton emailMirskyButton = new emailMirskyButton(mazeWorld);
        goPartyButton goPartyButton = new goPartyButton(mazeWorld);
        
        addObject(allNighterButton,517,399);
        addObject(twoAllNightersButton,517,399);
        addObject(emailMirskyButton,517,399);
        addObject(goPartyButton,517,399);
        
        allNighterButton.setLocation(394,241);
        questionOne.setLocation(409,100);
        twoAllNightersButton.setLocation(394, 325);
        emailMirskyButton.setLocation(394, 407);
        goPartyButton.setLocation(394, 485);
        
    }
All Night Button Class(response page):
public class allNighterButton extends Actor
{   
   World mazeWorld; 
     public allNighterButton(World world){
       GreenfootImage myImage = getImage();
       int myNewH = (int)myImage.getHeight();
       int myNewW = (int)myImage.getWidth();
       myImage.scale(myNewW, myNewH);
       mazeWorld = world;
   }
   
    /**
     * Act - do whatever the allNighterButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Greenfoot.setWorld(new allNighterResponse(mazeWorld));
        
        }
    }
Continue button Class: (This is where if clicked on I want to bring the player back to the latest instance of the maze (MyWorld)
public class ContinueButton extends Actor
{
    World mazeWorld;
     public ContinueButton(World world){
       GreenfootImage myImage = getImage();
       int myNewH = (int)myImage.getHeight();
       int myNewW = (int)myImage.getWidth();
       myImage.scale(myNewW, myNewH);
       mazeWorld = world;
   }
    
    /**
     * Act - do whatever the ContinueButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Greenfoot.setWorld(mazeWorld);

        
        }
    }
danpost danpost

2021/11/5

#
I fail to see where a ContinueButton is created. Can you point that out for me?
Cs-studentX Cs-studentX

2021/11/5

#
Oh sorry my bad, I forgot to post that here:
public class allNighterResponse extends World
{
    World mazeWorld;
    /**
     * Constructor for objects of class allNighterResponse.
     * 
     */
    public allNighterResponse(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
        mazeWorld = world;
    }
    
    private void prepare(){
        allNighterResponseImage allNighterResponseImage = new allNighterResponseImage();
        addObject(allNighterResponseImage,406,247);
        allNighterResponseImage.setLocation(409,116);
        
        ContinueButton ContinueButton = new ContinueButton(mazeWorld);
        addObject(ContinueButton,517,399);
        
        ContinueButton.setLocation(394, 485);
    }
danpost danpost

2021/11/5

#
Okay. In all your world constructors, you are calling prepare prior to setting the mazeWorld field. It is no wonder the field values in your Actor objects are null.
You need to login to post a reply.