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

2017/2/5

Switching back to a previously instantiated world?

mehanix mehanix

2017/2/5

#
Hello! And sorry for asking so many questions here in such a short period of time. I'm trying to make a menu which leads to 2 gamemodes of my game, each with their sepparate worlds. After the player wins the game, i want to switch the world back to the menu world which I instantiated previously. How can I acheve this, if it is possible? I tried doing this:
    void checkIfVictory()
    {
        if(this.isAtEdge())
            //TODO show victory message
            {
               //resetting variables
               MyWorld.gamemode=-1;
               MyWorld.isPlaying=false;
               for(int i=0;i<4;i++)
                        ItemCounter.gems_taken[i]=0;
    
              int score;

              //switching to menu
                 MyWorld w = new MyWorld();
                 Greenfoot.setWorld(w);                
                // if (getWorld() instanceof MyWorld)
                
            }
    }
But this just creates a brand new menu and I don't really want that.
danpost danpost

2017/2/5

#
If you want to return to the same menu, then you need to have the menu world retained in a field that is accessible to the game mode worlds.
mehanix mehanix

2017/2/6

#
Thank you for the idea!! i've been struggling on this for the past 2 weeks!! With this, I believe my game is ready to be published. Expect to see it soon :) For other people who want to do this who stumble upon this post: The way i fixed this was by having the other worlds besides require a reference to the world they were created in. like this:
                    TAworld w = new TAworld(this);
                    Greenfoot.setWorld(w);  
then, in the 2nd world's constructor:
MyWorld ww;
Public TAworld(w)
{
...
ww=w;
...
}
Finally, when switching back to the menu world:
            Greenfoot.setWorld(ww);
Hope this will be of help to someone else! I know i really struggled with this one.
You need to login to post a reply.