I have an array list that contains tiles in it. When I place a new tile down, the new tile is placed over the old one, but the old one is still there and can cause problems. For example, there is a stone tile that the player cannot walk through. An air tile is placed there, supposedly letting the player walk through. However, the stone tile is never deleted, so the player cannot walk through since the stone tile is solid and causes collision. Can I just delete the tile out of the arraylist? I don't think I can because then the rest of the data in the list will "move over" and fill the hole where the old tile used to be. How could I remove the tile and place a new tile over it without causing the rest of the data to shift?
Asked
Active
Viewed 176 times
1
-
Why you can't replace stone with air ? – Anton Jan 02 '13 at 13:17
-
Be sure that this tile is only referenced in this list you're talking about. The most common fallacy is that there are different lists (e.g. graphics, collision, logic etc) that get accessed by the game logic, so you'd have to apply that change to all those lists involved. – JayC667 Jan 02 '13 at 13:34
-
@JayC667 theres only one list, and its for tile,s – Jan 02 '13 at 14:34
4 Answers
2
You should use set(int index, E element)
method of ArrayList.
Acc. to Javadocs:
Replaces the element at the specified position in this list with the specified element

Azodious
- 13,752
- 1
- 36
- 71
1
If you want to remove an item from an ArrayList
and still want the rest of the list to keep in place, simply replace it with null
, that's authorized :
myList.set(i, null);
Of course that means you'll have to test the value when using it.

Denys Séguret
- 372,613
- 87
- 782
- 758
0
You can use the set() method of the ArrayList to "replace" one object in the list with another.
tileList.set(index, newTileObject);
will replace the tile at index with the new tile object. Leaving all the other tiles in their original location in the list.

Vincent Ramdhanie
- 102,349
- 23
- 137
- 192
0
Use a Tile
object with an enum to store its state:
public class Tile {
public enum State = { WALL, OPEN };
private State state;
public void setState(State state) {
this.state = state;
}
}
When you need to swap between open space and wall, you just change its state:
tile.setState(State.WALL);

asgoth
- 35,552
- 12
- 89
- 98
-
-
1In this case yes, when changing the state would be more complex, i would stick with changeState(). Changed (pun intended) it. – asgoth Jan 02 '13 at 13:43