Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
        //* 
                try{
        tile = layer.getCell(x,y).getTile();

        if("sign".equals(tile.getProperties().get("tileType").toString())){
        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.

share|improve this question

1 Answer 1

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.

share|improve this answer
    
thanks mobo... I am working on this project alone. I really ignored this blunder mistake –  Vishal Kumar Jun 17 '13 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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