Question

I am well into creating my 2d remake of minecraft in java. I know it can be done well, orange451 on youtube inspired me to try and make this. I have all blocks on the map loading from text files, and when the game loads, it adds all of the blocks from the text files to an ArrayList. I created an algorithm for calculating the index of the block your cursor is on in the game, and added a MouseListener so that when i clicked it would replace the block with an air block (basically destroying the block). To replace the block in the ArrayList, I used the ArrayList set(index, obj) method. In theory, it should be working correctly, and it in a way does. The only problem is that it also creates a black space in the map a few blocks away. This is extremly frustrating, especially since I have come so far. ADDITIONAL INFO: I need a method that will replace the object in the ArrayList, or a better way to do it because my collision detection method also uses the ArrayList to detect a blocks position. PLEASE HELP ME! I cant post images but its setting the block to the air texture but creating a black square (a gap in the arraylist mabey?) near it. Because theres too much code to post, heres the source code for the whole project: Blockworld 2D Source

Was it helpful?

Solution

You're struggling with this because an ArrayList of objects that know their coordinate is an insane way to represent this 2d structure. It's unordered - you could reverse or shuffle your ArrayList and it would paint the same. It has O(N) update, as you have to search the ArrayList for an object of the appropriate coordinate before you can replace it. It can have more than one object with the same coordinate. It can be in a state where visible coordinates do not have corresponding objects at all -- which is what you've encountered, here.

PLEASE HELP ME

OK. Start with a two dimensional array (array, not ArrayList) of byte. Which allows you 256 kinds of block, and which allows your players to dig without constantly allocating memory with your new AirBlock(0, 0) madness. To draw the world, iterate over visible coordinates and map bytes to Bitmap or like.

Also: a 2d Minecraft already exists. It's called Terraria.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top