2014-01-09 100 views
3

我們使用libgdx在Android中編寫遊戲。這是一個塗鴉跳躍。透明度Libgdx Android

在遊戲的一個階段,我們希望通過降低圖片的透明度來逐步消除平板。我們測試了很多東西,但沒有任何工作。

batch.begin(); 
batch.draw(picture, posX, posY, width, heigth); 
bach.end(); 

我們也試圖與這一點,但它不工作:

batch.setColor(1, 0, 0, 1); 
+2

setColor的最後一個參數是alpha,你試過1.0f以下吧?因爲alpha = 1是完全透明的 – taytay

+0

我試過這個,但它不起作用:/ – user3178291

回答

2

SpriteBatch#setColor作品:你沒有改變alpha(最後一個值),這就是爲什麼它沒有爲你工作。

batch.begin(); 
batch.setColor(1, 1, 1, 0.5F); //0.5F is the alpha value that you want (0-1) 
batch.draw(picture, posX, posY, width, heigth); 
batch.setColor(1, 1, 1, 1); //change it back to full opacity white, for the other objects to render correctly 
bach.end(); 

如果你開始使用Sprite S,mrzli answer是要走的路。

+0

非常感謝,它的工作:D! 我認爲我的錯誤是我沒有把這個ligne'batch.setColor(1,1,1,1);' – user3178291

1

你可以使用一個雪碧,然後使用此方法設置阿爾法:

public static void setSpriteAlpha(Sprite sprite, float alpha) { 
    Color c = sprite.getColor(); 
    sprite.setColor(c.r, c.g, c.b, alpha); 
} 

該作品當然,因爲我在我的塗鴉跳轉克隆中使用它。

下面是一些例子用法:

batch.begin(); 
sprite.setBounds(posX, posY, width, height); 
setSpriteAlpha(sprite, 0.5f); 
sprite.draw(batch); 
batch.end(); 

如果它可以幫助你,你可以找到整個遊戲here的源代碼,並使用它幾乎要(MIT許可證)的任何方式。這很簡單,可能沒有多少樂趣,但它已完成。

您還可以在Google Play上查看結果。

+0

沒有必要讓一個方法獲得Sprite顏色來修改它的alpha,你可以簡單地使用[Sprite#setAlpha](http:// libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setAlpha%28float%29) – Lestat

+0

@Lestat你說得對,我什至不記得我爲什麼開始做就這樣。我想我沒有看到這種方法... – mrzli

+0

非常感謝您的幫助!我會嘗試你的解決方案;) PS:我已經在Play商店下載你的遊戲,你的遊戲非常棒!你做得很好^^ – user3178291