Tell me this question: Packed the textures, connected the atlas and actually initialized the images, but by clicking on the old, the application crashes with an error: on a null object reference and swears at this 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; 

It is necessary to create an array of Sprite of these images, and then draw on the screen like this:

 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 array.

  • And the good old ArrayList <Sprite> does not suit you? - Victor Evlampiev
  • @Victor Evlampyev and how then to output game.batch.draw? - upward

1 answer 1

What 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 go?

  • Of course, I will try - upward
  • crashes with an error - upward
  • And what a mistake? If you use Android Studio, below there is the tab "Android Monitor" (it should be activated when you run the program on the device or emulator), there everything that is highlighted in red is an error (usually starts with "E /"). - nurchi