Domanda

Sono bene nel creare la mia 2d remake di Minecraft in Java. So che può essere fatto bene, orange451 su youtube mi ha spinto a cercare di fare questo. Ho tutti i blocchi sul caricamento carta da file di testo, e quando il gioco carica, aggiunge tutti i blocchi dai file di testo a un ArrayList. Ho creato un algoritmo per il calcolo dell'indice del blocco il cursore si trova nel gioco, e ha aggiunto un MouseListener in modo che quando ho cliccato sarebbe sostituire il blocco con un blocco d'aria (in pratica distruggendo il blocco). Per sostituire il blocco nel ArrayList, ho usato il set ArrayList metodo (indice, obj). In teoria, dovrebbe funzionare correttamente e in modo fa. L'unico problema è che si crea anche uno spazio nero nella mappa a pochi isolati di distanza. Questo è estremamente frustrante, soprattutto perché sono venuto finora. INFORMAZIONI AGGIUNTIVE: Ho bisogno di un metodo che andrà a sostituire l'oggetto nel ArrayList, o un modo migliore per farlo perché il mio metodo di rilevamento delle collisioni utilizza anche l'ArrayList per rilevare una posizione di blocchi. MI AIUTI PER FAVORE! Non posso immagini post, ma la sua impostazione il blocco alla texture aria, ma la creazione di un quadrato nero (una lacuna nel Mabey ArrayList?) Vicino ad esso. Perché theres troppo codice per posta, ecco il codice sorgente per l'intero progetto: Blockworld Fonte 2D

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top