2016-07-13 33 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