Sanity Check - Tiled Maps & The Player Sprite

Hi! Technically my first post here, though I’ve posted here under another name. I need some help with my code, and a second pair of eyes would probably help me solve this issue a bit faster.

What I’m trying to do is to get my player to collide with my map that I’ve designed in Tiled, following dermetfan’s Tiled Maps Tutorial -

https://www.youtube.com/watch?v=DOpqkaX9844].

I’ve come to a bit of a stumbling block, though:

http://pastebin.java-gaming.org/6115946892217

My program is NullPointer-ing out where it’s looking for the Width and the Height of the MetaLayer.

Would somebody tell me what I’m doing wrong? Am I insane?

I tested here with my own TiledMap and it’s working.

Maybe your layer’s name is wrong (lower/upper case):
.get(“MetaLayer”));

Thanks for the reply!

I’m actually at work right now, so I’ll try to see if it’s a case-sensitive issue when I get home. I’ll update this post if I’ve come to a solution, or… hit another roadblock.

MetaLayer is not initialised with the variable declaration which leaves it null, then you try to use it when assigning tileWidth and tileHeight. These actions precede the constructors actions. Additionally, your constructor doesn’t assign MetaLayer to Meta, you’re setting MetaLayer to MetaLayer, and you have the same problem with setMeta(). :slight_smile:

Also it’d be good to get out of the habit of starting variables with upper case letters, which are generally used for Class names by convention. It prevents confusion for both us and the syntax highlighter. :wink:

Thanks for for responding, guys!

So, I checked this when I got home, but I had gotten the layer name correct. No luck on this end. =[

So, I did as you suggested, and separated out all of the variables and gave them each a declaration. I also made sure that my naming conventions weren’t confusing.

http://pastebin.java-gaming.org/159696287261d

Unfortunately, I’m still NPE’ing at tileWidth and tileHeight. =[

I just tested your way and it’s working for me.

public class GameMap extends ScreenInterfaceTest{
	private TestClass test;
	private TiledMap map;
	
	@Override
    public void show(){
		map = new TmxMapLoader().load("maps/map.tmx");
		test = new TestClass((TiledMapTileLayer) map.getLayers().get("layer1"));

		System.out.println(test.getMeta().getTileWidth());
	}
}

///////////////

public class TestClass{
	private TiledMapTileLayer metaLayer;
	
	public TestClass(TiledMapTileLayer metaLayer){
        this.metaLayer = metaLayer;
    }

	public TiledMapTileLayer getMeta(){
		return metaLayer;
	}
}

My tile is 64x64, this is printing 64 on console.

This is weird, maybe the problem can be your map.
Can you render the map using renderer.render()?
Can you try create a TiledMapTileLayer in your screen class and test?

Here my .tmx code (header).

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="left-down" width="12" height="7" tilewidth="64" tileheight="64" nextobjectid="229">
 <tileset firstgid="1" name="tiled2" tilewidth="64" tileheight="64">
  <image source="tiled2.png" trans="ff00ff" width="960" height="1024"/>
 </tileset>
 <layer name="layer1" width="12" height="7">

Yeah, I’m able to render the map, but it wasn’t until I added the TiledMapTileLayer to the Player’s constructor in the Player class that I started having issues with the map. =/

You still have the same problem.

The following:


private TiledMapTileLayer metaLayer;
float oldY = getY();
float oldX = getX();
float tileWidth = metaLayer.getTileWidth();
float tileHeight = metaLayer.getTileHeight();

all happens before the constructor runs. Which means when you try call metaLayer.getTileWidth(), metaLayer is null, the constructor assigning to it hasn’t been called yet. Call getTileWidth()/Height() after you assign to metaLayer in the constructor.

Forgive me, I’m having a bit of trouble understanding what you mean… are you saying to place my variables inside of the player constructor, after metaLayer is called?

MASSIVE EDIT:

Alright! I got it working, somewhat…

Now I have to clean up my collision code some. Thank you guys for your help!