Search code examples
androidcolorslibgdx

How to set random color


this is an extract of my code:

Color[] color = new Color[3];
color [0] = Color.red;
color[1] = Color.blue;
color[2] = Color.yellow;
stage.getBatch().setColor(color[rand.nextInt()]);

But "color[rand.nextInt()]);" is underlined red. I really don´t know why. There have to be four numbers or instead "Color.BLUE" for example in the brackets but I want to tint the sprite randomly. Therefore I created an array with three colors. I thought by just giving them numbers and using rand.nextInt it would work. What is the mistake?


Solution

  • You can generate random colors like this:

    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));