Sprites array merges the when rendering sprites

浪子不回头ぞ 提交于 2019-12-14 04:09:04

问题


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

I create objects in the method called spawnRaindrop(). I have an array of sprites and I want to sprites were changed in the cycle as it is now, it works, but the images are merged with each other.

  sprites = new Sprite[15];
    r1 = new Sprite(atlas.findRegion("r1"));
    r1.flip(false, true);
    r2 = new Sprite(atlas.findRegion("r2"));
    r2.flip(false, true);
    r5 = new Sprite(atlas.findRegion("r5"));
    r5.flip(false, true);
    r10 = new Sprite(atlas.findRegion("r10"));
    r10.flip(false, true);
    r20 = new Sprite(atlas.findRegion("r20"));
    r20.flip(false, true);
    r50 = new Sprite(atlas.findRegion("r50"));
    r50.flip(false, true);
    r100 = new Sprite(atlas.findRegion("r100"));
    r100.flip(false, true);
    r200 = new Sprite(atlas.findRegion("r200"));
    r200.flip(false, true);
    r500 = new Sprite(atlas.findRegion("r500"));
    r500.flip(false, true);
    k1 = new Sprite(atlas.findRegion("k1"));
    k1.flip(false, true);
    k2 = new Sprite(atlas.findRegion("k2"));
    k2.flip(false, true);
    k5 = new Sprite(atlas.findRegion("k5"));
    k5.flip(false, true);
    k10 = new Sprite(atlas.findRegion("k10"));
    k10.flip(false, true);
    k20 = new Sprite(atlas.findRegion("k20"));
    k20.flip(false, true);
    k50 = new Sprite(atlas.findRegion("k50"));
    k50.flip(false, true);
    sprites[0] = r1;
    sprites[1] = r2;
    sprites[2] = r5;
    sprites[3] = r10;
    sprites[4] = r20;
    sprites[5] = r50;
    sprites[6] = r100;
    sprites[7] = r200;
    sprites[8] = r500;
    sprites[9] = k1;
    sprites[10] = k2;
    sprites[11] = k5;
    sprites[12] = k10;
    sprites[13] = k20;
    sprites[14] = k50;

Create game object

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

Create and draw array sprite

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

    for (Rectangle raindrop : raindrops) {
        for (int i = 0; i < sprites.length - 1; i++) {

            game.batch.draw(sprites[i], raindrop.x, raindrop.y);
        }
    }
    game.batch.end();

RESULT: I attached the picture, and it can be seen that the images accumulate on each other


回答1:


It looks like your problem is that you are using the same raindrop.x and raindrop.y coordinates for all sprites!

for (Rectangle raindrop : raindrops)
{
    for (int i = 0; i < sprites.length - 1; i++)
    {
        // The following will draw ALL sprites at the same location!
        game.batch.draw(sprites[i], raindrop.x, raindrop.y);
    }
}

What you can try is to create a new class called (for example): Raindrops and then in this class maintain a single x,y coordinate for each individual image:

class Raindrop
{
    Vector2 coordinates;
    Sprite sprite;
}

Then in your spawnRaindrop method, create an array of these Raindrop's and an individual (random?) image for each.

EDIT: I wrote the following code directly here without testing anything, so it will most likely have some errors, but nothing you shouldn't be able to fix yourself...

// This goes into your initialisation method
String regions[] = {"r1", "r2", "r5", "r10", "etc etc etc"}
Raindrop raindrops[] = new Raindrop[15];
for ( int i = 0; i < raindrops.length; i++ )
{
    raindrop[i] = new Raindrop();
    raindrop[i].coordinate.x = MathUtils.random(screenWidth);
    raindrop[i].coordinate.y = MathUtils.random(screenHeight);
    raindrop[i].sprite = atlas.findRegion(regions[(int)MathUtils.random(regions.length)]);
}

Then your main loop should look something like this:

game.batch.draw(bucketImage, bucket.x, bucket.y);
for ( Raindrop raindrop : raindrops )
{
    game.batch.draw(raindrop.sprite, raindrop.coordinate.x, raindrop.coordinate.y);
}
game.batch.end();


来源:https://stackoverflow.com/questions/38342642/sprites-array-merges-the-when-rendering-sprites

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!