How do I get/call a variable from one world to another?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (Nosson1459)
* @version (12/6/2016)
*/
public class MyWorld extends World
{
public int variable=4;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
}
public void act()
{
variable++;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld2 here.
*
* @author (Nosson1459)
* @version (12/6/2016)
*/
public class MyWorld2 extends World
{
/**
* Constructor for objects of class MyWorld2.
*
*/
public MyWorld2()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
}
public void act()
{
// on this line and any more needed below it, should be the coding for retrieving and using a variable from a different world
}
}
// replace line 11 with public static int variable; // and insert the following at line 20 variable = 4;
int var = MyWorld.variable;
MyWorld.variable = 0;