Should I make the Cells in a Tiledmap as null when my player hits it
- by Vishal Kumar
I am making a Tile Based game using Libgdx. I took the idea from SuperKoalio platformer demo by Mario Zencher. When I wanted to implement Collectables in my game , I simply draw the coins using Tiled Map Editor. When my player hits that, I use to set that cell as null.
Someday on this site suggested me not to do so... never use null. I agreed. What can be any other way. If I am using layer.setCell(x,y) to set the cell to any other cell... even if an transparent one .. my player seems to be stopped by an invisible object/hurdle.
This is my code:
for (Rectangle tile : tiles)
{
if (koalaRect.overlaps(tile))
{
TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1);
try{
type = layer.getCell((int) tile.x, (int) tile.y).getTile().getProperties().get("tileType").toString();
}
catch(Exception e){
System.out.print("Exception in Tiles Property"+e);
type="nonbreakable";
}
//Let us destroy this cell
if(("award".equals(type))){
layer.setCell((int) tile.x, (int) tile.y, null);
listener.coin();
score+=100;
test = ""+layer.getCell(0, 0).getTile().getProperties().get("tileType");
}
//DOING THIS GIVES A BAD EFFECT
if(("killer".equals(type))){
//player.health--;
//layer.setCell((int) tile.x, (int) tile.y, layer.getCell(20,0));
}
// we actually reset the player y-position here
// so it is just below/above the tile we collided with
// this removes bouncing :)
if (player.velocity.y > 0)
{
player.position.y = (tile.y - Player.height);
}
Is this a right approach? OR
I should create separate Sprite Class called Coin.