I am working on a game, it is my first time...I encountered a issue, that I cant help myself to solve. Lets cut to the chase, I have a coin class, It draws coins to the only one box2D body, but In that layer(which I made in tiled), I have more 1 body but coin appears in only 1 of them, the last one. I want the coin texture to appear in all the object layers I defined for coins,I will also post the picture to help understand my question better.Code and image for coin is give below;
Image : https://ibb.co/nAoYFq
public class Coin extends Sprite{
protected PlayScreen screen;
private Body body;
private BodyDef bodyDef;
private FixtureDef fixtureDef;
private PolygonShape polygonShape;
public Coin(PlayScreen screen, World world,TiledMap map) {
super(screen.getAtlas().findRegion("Gold"));
this.screen = screen;
this.bodyDef = new BodyDef();
this.fixtureDef = new FixtureDef();
this.polygonShape = new PolygonShape();
TextureRegion coinTexture = new TextureRegion(getTexture(),0,0,64,64);
setBounds(0, 0, 84 / trollVersusZombies.PPM, 84 / trollVersusZombies.PPM);
setRegion(coinTexture);
for(MapObject mapObject: map.getLayers().get(6).getObjects().getByType(RectangleMapObject.class))
{
Rectangle rectangle = ((RectangleMapObject)mapObject).getRectangle();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set((rectangle.getX() + rectangle.getWidth() / 2)/ trollVersusZombies.PPM, (rectangle.getY() + rectangle.getHeight() / 2) / trollVersusZombies.PPM);
body = world.createBody(bodyDef);
polygonShape.setAsBox(rectangle.getWidth() / 2 / trollVersusZombies.PPM, rectangle.getHeight() / 2 / trollVersusZombies.PPM);
fixtureDef.shape = polygonShape;
fixtureDef.isSensor = true;
body.createFixture(fixtureDef);
}
}
public void update()
{
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
}
}
FYI :
In my main playscreen class,I have declared and instantiated my coin class to pass revelant parameters and the update method of coin class is called in update method of main playscreen class, also in render method of playscreen class, I have called coin.draw(playscreen.batch), i.e;
public void update(float dt) {
//Other code...
coin.update();
}
public void render(float delta) {
//Other Code
gameScreen.batch.begin();
coin.draw(gameScreen.batch);
gameScreen.batch.end();
}