0
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

You should create a separate Sprite class for Coin. You don't necessarily have to do this, but it will make it much easier to create other collectables, or over other kinds of tiles, whereas it sounds like the way you are doing it now, you will have to create a different tile whenever the terrain changes, such as coin-over-grass, coin-over-sand etc.

Your tiles will now have a member, that is a reference, which tracks what kind of collectible is on it, if any. It will be a reference to a collectable, which could be a Coin. If there is no collectable, you could set it to null or maybe a special NullCollectable singleton.

interface Collectable {
    void collectBy(Listener listener);
}

class Coin implements Collectable {
    public void collectBy(Listener listener) {
        listener.coin();
    }
}

class Tile {
    private Collectable collectable;
    public stepOnBy(Listener listener) {
        if (collectable != null) {
            collectable.collectBy(listener);
            collectable = null;
        }
    }
}

You may have heard "never use null", but it's important to know the reason behind such statements. The reason is that null forces you to check for null every time you use that type, because not doing so will trigger NullPointerExceptions, which tend to be useless because they often do not show you the source of the problem - where the object became null. People often use null as either a sentinel value or a placeholder for activating default behaviour. A good pattern to use instead is the Null Object Pattern, where you put your default behaviour in a single class and use that instead of null.

For example, instead of checking null in Tile.stepOnBy(), I can use a NothingCollectable instead:

class NothingCollectable implements Collectable {
    private static final NothingCollectable instance = new NothingCollectable();
    public static NothingCollectable getInstance() {
        return instance;
    }
    public void collectBy(Listener listener) {
        // do nothing
    }
}

class Tile {
    private Collectable collectable;
    public stepOnBy(Listener listener) {
        collectable.collectBy(listener);
        collectable = NothingCollectable.getInstance();
    }
}
\$\endgroup\$
  • \$\begingroup\$ It went beyond my head. I understand just a little. \$\endgroup\$ – Vishal Kumar Jul 1 '13 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.