I am making a tile based game in LibGdx. I am using the TiledMap in .tmx format. I want when my player reaches a tile whose property is set (tileType -> sign), he should see a Sign sprite and on colliding which, he should see the signScreen (small) in the Parent screen game screen. Here is my code

//calling this method at the beginning ...in create()

    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1);
    //test ="layer selected :-> "+layer;
    TiledMapTile tile;

        for(int y=0; y< mapHeight;y++){
            for(int x=0;x<mapWidth;x++){

        //Step-1: Search within the level map where should we construct a Sign
        tile = layer.getCell(x,y).getTile();

        layer.setCell(x, y, null);

//creating my sign sprite deleting that tile .. at the same position signs.add(new Sign(x , y , signCount++));


        catch(NullPointerException e){
            System.out.println("While Searching Tile for Sign ->"+e);

But this code is not working. I checked ...things look Ok. But if I am placing this code in the collision detection code, where I am checking collision of player with the Layer of map. It works. But I do not want to put the code there.Because, if I am searching and replacing for the tile there..it outputs as if...........

"when the player reaches there and touchs the sign, then my Sign sprite becomes visible and at the same time, the child screen appears"....But I want all the tiles that I have marked as (tileType -> sign) should be checked at the beginning and replaced with my Sign Sprite.

Please suggest

Is there any problem with the above code or the loop Is there is any other way to implement such a sprite that can communicate to player through a small screen .. i.e. illusion of telling something.


First off I would really try to avoid using null in game logic. Inherently it strips away all information about the state. Does null mean nothing is there? There was a failure? There was a success and no problem? Its vague. You are probably the only one working on your project, so you know what your wrote. But no one else will, and you probably won't if you come back to this project 6 months from now.

I would make your collision engine detect multiple things, not just invalid tiles. You could make an enum of collision types like : offscreen, sign, etc. Also you don't have to remove the tile to put a sign there. If you render the tilemap first you can then just draw the sign sprite over it. Or you could even put the sign in your tilemap if you want.

  • \$\begingroup\$ thanks mobo... I am working on this project alone. I really ignored this blunder mistake \$\endgroup\$ – Vishal Kumar Jun 17 '13 at 16:25

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.