Sprites数组合并渲染精灵时

时间:2016-07-13 04:22:21

标签: java libgdx

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

我在名为spawnRaindrop()的方法中创建对象。我有一个精灵数组,我想在周期中更改精灵,因为它现在,它的工作原理,但图像是相互合并的。

  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;

创建游戏对象

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

创建并绘制数组精灵

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

结果: 我附上了图片,可以看出图像相互累积

enter image description here

1 个答案:

答案 0 :(得分:1)

看起来您的问题是您为所有精灵使用相同的raindrop.xraindrop.y坐标!

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

您可以尝试创建一个名为(例如)Raindrops的新类,然后在此类中为每个单独的图像维护单个x,y坐标:

class Raindrop
{
    Vector2 coordinates;
    Sprite sprite;
}

然后在你的spawnRaindrop方法中,为每个创建一个Raindrop数组和一个单独的(随机?)图像。

编辑:我直接在这里编写了以下代码而没有进行任何测试,所以很可能会有一些错误,但是你不应该自己解决这个问题......

// 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)]);
}

然后你的主循环应该是这样的:

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