简体   繁体   中英

Libgdx Adding sprites to array and drawing them to screen

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);

Because the constructor of the Gold class does not call the superclass's ( Sprite ) constructor, the default constructor is called implicitly. If you take a look at the documentation of the default constructor of Sprite you'll see it specifically notes that:

... Creates an uninitialized sprite. The sprite will need a texture region and bounds set before it can be drawn .

So that explains why nothing is being drawn. One way to fix this is to set a Texture Region . So in the Gold constructor add the following:

setRegion(gold);

There are different solution too. It all depends what you're trying to accomplish.

I noticed that your Gold class, which is a Sprite, also has a reference to some other sprite, and that other sprite is the one you have set a region to, and is not the one you are drawing. Remove all references to another sprite in your Gold class.

Also, it would be better not to have a reference to a SpriteBatch or your TiledMap inside your Sprite. That introduces unnecessary coupling that could lead to bugs letter, or just make it harder to maintain your code as it gets more complicated.

public class Gold extends Sprite {
    private boolean isCollected; //only use a primitive wrapper if you really need one

    public Gold(Rectangle bounds, Texture gold) {
        super(gold);

        setSize( bounds.width / MarioBros.PPM, bounds.height / MarioBros.PPM);
        setPosition(bounds.x / MarioBros.PPM, bounds.y / MarioBros.PPM);
        isCollected = false;
    }
//...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM