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

2016/6/7

switching worlds

benbec1 benbec1

2016/6/7

#
I want to switch the world form the title screen, it runs but it doesn't work. Here is the code:
import greenfoot.*;
import java.awt.Color;

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

    /**
     * Constructor for objects of class world.
     * 
     */
    public world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 500, 1); 

        
        
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    public void act()
    {
        world Main = new world();
        if(Greenfoot.isKeyDown("e"))
        Greenfoot.setWorld(Main);
    }
}
danpost danpost

2016/6/8

#
You will not visually be able to tell whether the world changed or not because you are re-creating a copy of the same world. You could right click on the world after compiling and select 'Inspect' from the options; then run the scenario, press the "e" key and then pause the scenario; finally, right click on the world and select 'Inspect' again. At that point, if you have two "Inspect" windows open then they were different worlds. You should probably combine line 32 with line 34 so that you are not unnecessarily creating a new 'world' world every act cycle:
Greenfoot.setWorld(new world());
You need to login to post a reply.