Collision between different maps (2d lwjgl)

[quote]I’m gonna stay away from static variables from now on xD.
[/quote]
Fwiw, I use static variables all the time. If you only ever need one at a time, static makes sense. Some people put it in an instance anyway (someone mentioned that here), and I’m sure there are reasons for that, but I have not yet felt the need to do so with stuff where you only need one of it. (Constants like TILE_WIDTH, settings like mouseSensitivity, classes full of utility functions, etc. I don’t need to instantiate multiple LibMaths, so I just make everything in it static and go LibMath.thing() instead of lm = new LibMath(); lm.thing().)

But if you want something to have multiple versions of it floating around (like having multiple different levels floating around), you’re got the right idea: make instances instead of separate classes with static stuff.

Regarding one-instance objects, instead of making them static you can use the singleton design pattern. A singleton only ever has one instance of itself, ever.

Here’s an example taken from my Blocks class, which is a singleton of every block in the game.


private static Blocks instance;

	private Blocks() {
	}

	public static Blocks instance() {
		if (instance == null) {
			instance = new Blocks();
			instance.loadResources();
		}
		return instance;
	}

	private HashMap<String, Block> blocks = new HashMap<String, Block>();
	private HashMap<Block, String> reverse = new HashMap<Block, String>();
	private Array<Block> allBlocks = new Array<Block>();

	private void loadResources() {
		put(defaultBlock, new BlockEmpty());
		put("stone",
				new BlockStone().solidify(BlockFaces.ALL).setOpaqueToLight()
						.addAnimation(Block.singleBlockTexture("images/blocks/stone.png")));
		put("dirt",
				new BlockDirt().solidify(BlockFaces.ALL).setOpaqueToLight()
						.addAnimation(Block.singleBlockTexture("images/blocks/dirt.png")));
		put("grass",
				new BlockGrass().solidify(BlockFaces.ALL).setOpaqueToLight()
						.addAnimation(Block.singleBlockTexture("images/blocks/grass.png"))
						.addAnimation(Block.singleBlockTexture("images/blocks/dirt.png")));
		put("tall_grass", new BlockTallGrass().addAnimation(Block
				.singleBlockTexture("images/blocks/tall_grass.png")));
	}

	public void put(String key, Block value) {
		blocks.put(key, value);
		reverse.put(value, key);
		allBlocks.add(value);
	}
        // rest omitted for brevity
}

To access the Blocks instance, you can call Blocks.instance() which will either create the instance (that’s private static) if it’s null or return the instance.