In this following code I have tried to create a coin class and make it so it is a sprite. I have tried to add all these sprites to an array and then draw the array to the screen. It does not produce any errors just does not print the graphic to the screen. I also wanted to know if i could use the sprite to test for collisions. I know my code is not very good as it is quite messy and im just trying to find a solution. Thanks
public class Gold extends Sprite {
private SpriteBatch batch;
private TiledMap map;
private Sprite sprite;
private Boolean isCollected;
public Gold(TiledMap map, Rectangle bounds, Texture gold) {
this.map = map;
sprite = new Sprite(gold);
sprite.setSize( bounds.width / MarioBros.PPM, bounds.height / MarioBros.PPM);
sprite.setPosition(bounds.x / MarioBros.PPM, bounds.y / MarioBros.PPM);
isCollected = false;
}
for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
for(int i = 0; i < map.getLayers().get(5).getObjects().getCount() - 1; i++){
goldArray[i] = new Gold(map, rect, gold);
}
}
public void drawGold(TiledMap map){
for(int i = 0; i < map.getLayers().get(5).getObjects().getCount() - 1; i++){
goldArray[i].draw(batch);
}
}
In the render:
mapCreator.drawGold(map);
EDIT - I acted upon the first 2 suggestions and now the program outputs this error
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.alexcz.mariobros.Tools.MapCreator.<init>(MapCreator.java:77)
on this line goldArray[i] = new Gold(rect, gold);