How to set a layer's background color randomly

Get help using Construct 2

Post » Sat May 04, 2013 3:27 am

Hello,

I'm trying to change a layer's background color to random colors.
I have 3 global variables: r, g, and b.
I have a System event set for every 10 seconds.

I then have 4 System actions:

Set b to random(254)
Set g to random(254)
Set r to random(254)
Set layer "background" background color to rgb(r,g,b)

But when I run the game the background does not change.
Any advice? I looked but didn't find a Layer Refresh or anything like that.

I'm using 254 in my random because I'm assuming it's zero based, please correct me if I'm wrong.

Thank you!
B
10
S
2
Posts: 16
Reputation: 962

Post » Sat May 04, 2013 6:30 am

@wolfmanyoda:
"I then have 4 System actions:

Set b to random(254)
Set g to random(254)
Set r to random(254)
Set layer "background" background color to rgb(r,g,b)

But when I run the game the background does not change."

Maybe using floor(random(254)) for each, since the Random function will not return only integers(in fact, they have almost no chance of returning an integer)

Also, check that your layer is not transparent
https://www.scirra.com/forum/viewtopic.php?t=152506

And that is why you shall respect the bug reports guidelines, not only giving a capx is making the bug reproductible in one click in a situation they can work with (less time wasted trying to reproduce vague instructions) but also it helps filtering false positives.

Game design is all about decomposing the core of your game so it becomes simple instructions.
B
42
S
17
G
16
Posts: 2,094
Reputation: 15,355

Post » Sat May 04, 2013 10:41 am

try this

Set b to round(random(254))
Set g to round(random(254))
Set r to round(random(254))
Set layer "background" background color to rgb(r,g,b)
B
18
S
6
G
3
Posts: 197
Reputation: 4,129

Post » Sat May 04, 2013 12:21 pm

Awesome, thank you both.

The layer was transparent, so changing that fixed it.
B
10
S
2
Posts: 16
Reputation: 962

Post » Sat May 04, 2013 1:05 pm

Also, I'd recommend doing it in one event, and just set the background colour to rgb(floor(random(254)), floor(random(254)), floor(random(254))). Unless you need those individual RGB values for something else, of course.
B
56
S
15
G
10
Posts: 910
Reputation: 11,972

Post » Sat May 04, 2013 2:21 pm

Cool, I didn't realize I could do it that way, thank you.
B
10
S
2
Posts: 16
Reputation: 962

Post » Sat May 04, 2013 6:57 pm

floor(random(256)) since it's a value between 0 and 255 inclusive.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020


Return to How do I....?

Who is online

Users browsing this forum: YoHoho and 11 guests