I am producing a game for a school assignment, and have decided to make several worlds that the player can traverse by moving to the edge of the screen (like original Zelda pretty much) and want to be able to do this without creating a new instance of a world every time the player enters it. I want to declare an instance of every area once, and load that instance every time that area is accessed, but where and how do i do that? Does anyone have a suggestion for a better way to achieve the same result?
Here is what the current code for a world looks like:
As you can see, I call the moveWorlds() function from my Player class and every world has a constructor that places the player in the appropriate position at the edge, creating a recursive system that begins with the initiation of the first world from the main menu world.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class World2 here. * * @author (your name) * @version (a version number or a date) */ public class World2 extends World { /** * Constructor for objects of class World2. * */ public World2(int x, int y) { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(640, 640, 1); prepare(x, y); } private void prepare(int x, int y) { setPaintOrder(Bg2.class, Bg2Mid.class, Player.class); Bg2 Bg2 = new Bg2(); addObject(Bg2,320,320); Bg2Mid Bg2Mid = new Bg2Mid(); addObject(Bg2Mid,320,320); Player Player = new Player(); addObject(Player,x,y); } static public void moveWorlds(int edge, int x, int y){ if (edge == 0){ return; } if (edge == 1){ Greenfoot.setWorld(new Lake (x, 640)); } if (edge == 2){ Greenfoot.setWorld(new MyWorld(0, y)); } if (edge == 3){ Greenfoot.setWorld(new Lake(x, 0)); } if (edge == 4){ Greenfoot.setWorld(new Lake(640, y)); } } }