简体   繁体   English

Libgdx将精灵添加到数组并将其绘制到屏幕

[英]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); 在这行上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. 因为Gold类的构造函数没有调用超类的( Sprite )构造函数,所以默认构造函数被隐式调用。 If you take a look at the documentation of the default constructor of Sprite you'll see it specifically notes that: 如果查看Sprite的默认构造函数的文档 ,您会发现它特别指出:

... 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 . 解决此问题的一种方法是设置“ Texture Region So in the Gold constructor add the following: 因此,在Gold构造函数中添加以下内容:

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. 我注意到您的Gold类(即Sprite)也引用了其他一些Sprite,而其他Sprite是您将区域设置为该Sprite,而不是您正在绘制的Sprite。 Remove all references to another sprite in your Gold class. 删除对Gold类中另一个Sprite的所有引用。

Also, it would be better not to have a reference to a SpriteBatch or your TiledMap inside your Sprite. 另外,最好不要在Sprite中引用SpriteBatch或TiledMap。 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;
    }
//...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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