2016-07-13 103 views
0

Sprite bucketImage,background,r1,r2,r5, r10,r20,r50,r100,r200,r500,k1,k2,k5,k10,k20,k50;Sprites数组合并渲染精灵的时间

我在名为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

+0

那么你的问题到底是什么? –

+0

@ Edvin Tenovim 如何通过game.batch.draw(sprites,raindrop.x,raindrop.y)绘制一个sprit数组;正确??? – upward

回答

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方法,创建一个这些雨滴的阵列和一个个人(随机?)图像为每个。

编辑:我写了下面的代码直接在这里没有测试任何东西,所以它很有可能会有一些错误,但你不应该能够解决自己没什么......

// 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(); 
+0

是的,我想随机画出精灵。如何实施它,帮助? – upward

+0

我的问题类似于此| http://stackoverflow.com/questions/30198058/libgdx-render-for-a-rectangle-array-different-textures – upward

+0

雨滴雨滴[] =新雨滴[15]; 我没有这个类 我创建了一个对象 Array 雨滴; 看看这个,我的例子 https://github.com/libgdx/libgdx/wiki/A-simple-game 为了促进雨滴的,我们将编写一个名为spawnRaindrop(方法),它实例化一个新的Rectangle创建,将其设置为屏幕顶部边缘的随机位置,并将其添加到雨滴阵列中。 – upward