Trafalgar
Trafalgar

Reputation: 145

How to randomly change color? Dart and StageXL

for(int i = 1; i < 10; i++) {
var shape = new Shape();

shape.graphics.beginPath();
shape.graphics.moveTo(r1.nextInt(1500), r2.nextInt(1500));
shape.graphics.lineTo(r3.nextInt(1500), r4.nextInt(1500));
shape.graphics.strokeColor(Color.Green);
shape.graphics.closePath();
stage.addChild(shape);
 }

How can i randomly change the color of the line?

Upvotes: 0

Views: 148

Answers (2)

filiph
filiph

Reputation: 3113

In StageXL, colors are actually just integers. So, as @bp74 says, you can do:

var a = 255; // Assuming you want full opacity every time.
var r = random.nextInt(256); // red = 0..255
var g = random.nextInt(256); // green = 0..255
var b = random.nextInt(256); // blue = 0..255
var color = (a << 24) + (r << 16) + (g << 8) + b;
shape.graphics.strokeColor(color);

This is assuming you have random defined somewhere above:

var random = new Random();

Note that you most probably don't need several Random instances (like you have with r1, r2, r3 and r4, I assume).

Upvotes: 1

bp74
bp74

Reputation: 418

The color in StageXL (and Flash) is a 32 ARGB value. You use 4x8 bit for the alpha, red, green and blue color channel. The following example shows the color 0xFFFFFFFF which is white:

var a = 255; // alpha = 0..255
var r = 255; // red = 0..255
var g = 255; // green = 0..255
var b = 255; // blue = 0..255
var color = (a << 24) + (r << 16) + (g << 8) + b;

Upvotes: 0

Related Questions