1
\$\begingroup\$

So, I'm attempting to change the texture of a block when the player comes in contact with it by using cell.setTile(tileSet.getTile(926). I've already looked at similar questions and I guess the layer in Tiled is coming up as null, but I have no clue how to fix it.

I'm sorry it's a long post.

Here's the World Creator

public class B2DWorldCreator {

public B2DWorldCreator(PlayScreen screen) {
    World world = screen.getWorld();
    TiledMap map = screen.getMap();

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();
    Body body;

    //create static bodies for every wall in the game
    for (RectangleMapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){
        new Wall(screen, object);


    }


}
}

Here's the Tile class

public class Tile {
protected World world;
protected TiledMap map;
protected Rectangle bounds;
protected Body body;
protected PlayScreen screen;
protected MapObject object;
protected Fixture fixture;

public Tile(PlayScreen screen, MapObject object) {
    this.screen = screen;
    this.map = screen.getMap();
    this.world = screen.getWorld();
    this.object = object;
    bounds = ((RectangleMapObject) object).getRectangle();

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();


    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(bounds.getX() + bounds.getWidth() / 2, bounds.getY() + bounds.getHeight() / 2);

    shape.setAsBox(bounds.getWidth() / 2, bounds.getHeight() / 2);
    fdef.shape = shape;

    body = world.createBody(bdef);
    fixture = body.createFixture(fdef);
}

public void setCategoryFilter(short filterBit) {
    Filter filter = new Filter();
    filter.categoryBits = filterBit;
    fixture.setFilterData(filter);

}
}

Here's the wall class where I call the problematic line at 31.

public class Wall extends Tile {
private TiledMapTileSet tileSet;
private TiledMapTileLayer layer;
private TiledMapTileLayer.Cell cell;

public Wall(PlayScreen screen, MapObject object){
    super(screen, object);
    fixture.setUserData(this);
    setCategoryFilter(AdventureCompany.WALL_BIT);
    tileSet = map.getTileSets().getTileSet("PlaceHoldernoTree.tsx");

}

public void onContact(){
    cell = new TiledMapTileLayer.Cell();
    if(object.getProperties().containsKey("Door")){
        layer = (TiledMapTileLayer) map.getLayers().get(1);
        cell = layer.getCell((int) body.getPosition().x / 16, ((int) body.getPosition().y / 16));
        System.out.println(cell.getTile());
        cell.setTile(tileSet.getTile(926));
    }

}
}

My Contact Listener

public class WorldContactListener implements ContactListener {
private AssetManager manager;


public WorldContactListener(AssetManager manager) {
    this.manager = manager;
}

@Override
public void beginContact(Contact contact) {
    //these are what came in contact
    Fixture fixA = contact.getFixtureA();
    Fixture fixB = contact.getFixtureB();

    int cDef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits; // this will be used to activate specific collision results
    Gdx.app.log("Contact", "ya boi");
    switch(cDef) {
        case AdventureCompany.EXPLORER_BIT | AdventureCompany.WALL_BIT:
            if(fixA.getFilterData().categoryBits == AdventureCompany.WALL_BIT)
                ((Wall) fixA.getUserData()).onContact();
            else
                ((Wall) fixB.getUserData()).onContact();


            break;
    }


}

And finally, the Tmx file header

    <?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.1.5" orientation="orthogonal" renderorder="right-down" width="64" height="64" tilewidth="16" tileheight="16" infinite="0" nextobjectid="135">
 <tileset firstgid="1" source="test6.tsx"/>
 <tileset firstgid="925" source="PlaceHoldernoTree.tsx"/>
 <layer name="Background" width="64" height="64">
  <data encoding="base64">
\$\endgroup\$
  • \$\begingroup\$ line 31 is not very helpful... please post your stacktrace... \$\endgroup\$ – Martin Frank Jul 16 '18 at 14:29
1
\$\begingroup\$

After a lot of headache, I figured out that my problem was that in my Wall class when I did tileSet = map.getTileSets().getTileSet("PlaceHoldernoTree.tsx"); the file type was unnecessary and only needed the file name "PlaceHoldernoTree".

\$\endgroup\$

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.