Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 25, 2013 3:18 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: How to find the wall?
PostPosted: Thu Mar 25, 2010 6:49 am 
Offline

Joined: Wed Jan 06, 2010 11:37 pm
Posts: 15
Hi!
I'm new with slick, and I want to train myself doing little game. My first goal now is to move my Image(Mario!) in my map, and stop the movement if there is a wall.

So to do it I did my map with MapEditor. I did 2 TileSet :
    Wall: I set a property(blocked) and I put the value 'yes'.
    Floor: It's my grass, where my character can walk. No porperty on it.


So no i can move my character in my map but he can move on the "wall" too. And I want to resolve it. So I tried this in my code:

Code:
public void update(GameContainer c, int delta) throws SlickException
    {
// I have an attibute call map, to call my tilemap.so the type of map is
//tiledmap.
        Input in=c.getInput();

        if (in.isKeyDown(Input.KEY_UP)){


int id=(int)x+1;
boolean blocked = "yes".equals(map.getTileProperty(id, "blocked", "non"));
if(!blocked)
    x++;

    }

}

But this code doesn't works. I have this error:

Code:
Thu Mar 25 07:47:17 CET 2010 ERROR:null
java.lang.NullPointerException
        at org.newdawn.slick.tiled.TiledMap.getTileProperty(TiledMap.java:297)
        at Main.update(Main.java:82)
        at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:657)
        at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
        at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
        at Main.main(Main.java:56)
Thu Mar 25 07:47:17 CET 2010 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.
        at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663)
        at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
        at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
        at Main.main(Main.java:56)

I don't understand..... what do I must to do?
Is there a better way? Why my code doesn't works?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2010 8:01 pm 
Offline

Joined: Wed Jan 06, 2010 11:37 pm
Posts: 15
Any one can help me?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2010 8:16 pm 
Offline
Regular

Joined: Tue Mar 03, 2009 11:53 pm
Posts: 123
Hi.

The first parameter of getTileProperty is the id of the tile, that is, roughly, it's position in the tileset. So you first have to recover the global id of the tile you want to check.
getTileId helps you to recover the ID of a tile from the map.
First, you hav to "gridify" your player's (future) coords, that is, converting them from screen or world space to map space.
Next, you call getTileId and get the ID of the tile in that place of the map.
Finally, you can check in that ID has the "blocked" property assigned.

Note that, according to Javadoc, getTileProperty "will not perform well and should not be used as part of the default code path in the game loop". You'd better build a matrix when loading the map, maybe assigning true to walkable tiles and false to those which are not, and check your player coords against this map.

Ciao!

_________________
Carotinho


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2010 8:55 pm 
Offline

Joined: Wed Jan 06, 2010 11:37 pm
Posts: 15
Ok thanks you very much! It's works now!
The problem was i think because I did getTileId and I gave float x and y.
^^


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group