3

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?

lelloman
  • 13,883
  • 5
  • 63
  • 85
user8340536
  • 209
  • 2
  • 7

3 Answers3

5

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));   
Ayush Khare
  • 1,802
  • 1
  • 15
  • 26
1

Given the error message Error message: "Cannot resolve method 'setColor (java.awt.Color)' it looks like you are using the wrong Color class for Libgdx's SpriteBatch.

You need to change your import to com.badlogic.gdx.graphics.Color

Also, to correctly randomly select from your 3 colors, you need to use rand.nextInt(color.length) to bound the randomly generated int to the number of elements in your array.

JK Ly
  • 2,845
  • 2
  • 18
  • 13
0

Have a look at this Semi-random approach that make more shiny and fresh colors.

enter image description here

ucMedia
  • 4,105
  • 4
  • 38
  • 46