How to set a layer's background color randomly

0 favourites
  • 7 posts
From the Asset Store
A whole set you need to create a gorgeous winter 2d game
  • 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!

  • 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

  • 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)

  • Awesome, thank you both.

    The layer was transparent, so changing that fixed it.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • Cool, I didn't realize I could do it that way, thank you.

  • floor(random(256)) since it's a value between 0 and 255 inclusive.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)