user8340536 user8340536 - 1 year ago 96
Android Question

How to change color of a sprite randomly

I wrote the following code:

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


With this code I want to change the color of a sprite randomly. Unfortunately, the color of all sprites on the stage get changed but I want to declare which sprite I want to tint. How can I improve my code?

Answer Source

I prefer to use in this way :

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download