Libgdx: when sprite elements of the mass are painted, error on a null object reference



  • Say this question: Packed the textures, connected the atlas and initiated the images themselves, but with the pressure on the old one, the app erroneously eject: on a null object referenceand fights on that line. game.batch.draw(sprite, raindrop.x, raindrop.y);

      r1 = new Sprite(atlas.findRegion("r1"));
        r2 = new Sprite(atlas.findRegion("r2"));
        r5 = new Sprite(atlas.findRegion("r5"));
        r10 = new Sprite(atlas.findRegion("r10"));
        r20 = new Sprite(atlas.findRegion("r20"));
        r50 = new Sprite(atlas.findRegion("r50"));
        r100 = new Sprite(atlas.findRegion("r100"));
        r200 = new Sprite(atlas.findRegion("r200"));
        r500 = new Sprite(atlas.findRegion("r500"));
        k1 = new Sprite(atlas.findRegion("k1"));
        k2 = new Sprite(atlas.findRegion("k2"));
        k5 = new Sprite(atlas.findRegion("k5"));
        k10 = new Sprite(atlas.findRegion("k10"));
        k20 = new Sprite(atlas.findRegion("k20"));
        k50 = new Sprite(atlas.findRegion("k50"));
    

    Sprite bucketImage, r1, r2, r5,
    r10, r20, r50, r100, r200, r500, k1, k2, k5, k10, k20, k50;

    There is a need to create a Sprite of these images, and then
    To paint on the screen:

    game.batch.draw(bucketImage, bucket.x, bucket.y);

    for (Rectangle raindrop : raindrops) {
        for (Sprite sprite : sprites) {
            game.batch.draw(sprite, raindrop.x, raindrop.y);
        }
    }
    

    private void spawnRaindrop() {
    Rectangle raindrop = new Rectangle();
    raindrop.x = MathUtils.random(0, 800 - 64);
    raindrop.y = 480;
    raindrop.width = 64;
    raindrop.height = 64;
    raindrops.add(raindrop);
    lastDropTime = TimeUtils.nanoTime();
    }
    game.batch.end();

    spawnRaindrop() creates a new Rectangle, sets it in a random position at the top of the screen and adds it to the raindrops.



  • How about:

    Sprite[] sprites=new Sprite[] {r1, r2, r5, ...};
    ...
    for (Rectangle raindrop : raindrops) {
        for (Sprite sprite : sprites) {
            game.batch.draw(sprite, raindrop.x, raindrop.y);
        }
    }
    

    Will it?




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2