2017-07-26 61 views
2

我寫了下面的代碼:如何改變精靈的顏色隨機

int color = Color.argb8888(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); 
stage.getBatch().setColor(color); 

有了這個代碼,我想隨意改變精靈的顏色。不幸的是,舞臺上所有精靈的顏色都變了,但我想聲明我想要着色的精靈。我如何改進我的代碼?

+0

每個演員都負責使用自己的繪製方法在批處理中設置其顏色。如果不應該着色,請使用Color.WHITE。 – Tenfour04

+0

'Sprite' with'Stage'不是好方法'Image'代替精靈 – Aryan

+0

但是我怎樣才能改變圖像的顏色?用同樣的方法? – user8340536

回答

0

我更喜歡以這種方式來使用:

final Image image=new Image(new Texture("badlogic.jpg")); 
final Color colors[]=new Color[]{Color.YELLOW,Color.RED,Color.CYAN}; 
image.addAction(Actions.forever(Actions.sequence(Actions.delay(.2f),Actions.run(new Runnable() { 
      @Override 
      public void run() { 
       image.setColor(colors[MathUtils.random(colors.length-1)]); 
      } 
     })))); 
stage.addActor(image); 
2

要改變一個精靈的色調,使用下面的代碼

Sprite mysprite; 
Texture mytexture; 
mytexture = new Texture("texture.png"); 
mysprite = new Sprite(mytexture); 
mysprite.setColor(Color.WHITE.cpy().lerp(Color.BLACK, .5f)); 

代碼的改變精靈的白色色調,以黑色爲主色調

+0

如果要隨機更改顏色,請將最後一行放入Random()函數 –

0

根據我的經驗,您可以將着色器中用作頂點屬性的顏色屬性設置回正常的顏色遮罩,如下所示:

stage.getBatch().setColor(your_random_color); 
stage.getBatch().draw(your_texture); 
stage.getBatch().setColor(1.0f,1.0f,1.0f,1.0f); 

請注意,我試圖通過這種方式繪製TextureRegion,但我認爲這不會是用繪圖雪碧不同。

1
Color color = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat(), 1f); 

「rand.nextFloat()」是爲您提供了在0和1之間的浮動,以便可以創建隨機顏色這樣的。

+1

中添加一些細節,而不是僅轉儲代碼並進行格式化。 –