How do I Store colours in an array and randomly select one?

  • I am currently building a test game where an array is populated with colour values in the format of: "255, 255, 255" and at the moment I have an array that has 5 colours inside of it.

    When the layout first loads, I am wanting to select a random colour from this array and then apply that to the background color property of a particular layer at index 0.

    At the moment I have a global variable called: Color and on layout start I set its value to a randomly picked colour from the array like so: Array.At(random(Array.Width))

    I then attempt to set the background colour of layer 0 to the variable, but nothing happens. My understanding is the layer background colour excepts a string of RGB values, but all I see is a consistent black background: no colour.

    Any examples of choosing colours from an array and setting a layer background colour from a randomly picked value would be fantastic. Thank you in advance.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Not sure why you use an array?.

    You can do it like this without the array:

    Event:

    Set layer background color

    Layer: The name of the layer you want to change color.

    Color: RGB(int(random(255)), int(random(255)), int(random(255)))

    You can just put it in a function and call it every time you have to change background color.

    Also you have to make sure that the Layer properties are set correctly.

    Transparent = No

    Opacity = 100

  • Thanks for the reply Nimos. I didn't explain myself well enough.

    I have an array of 5 pre-chosen colours I want to randomly use in my game as the layer background. The reason I use an array is because my understanding is there is nothing else fit for the purpose of storing multiple values and then selecting one.

    On start of layout

    Choose random colour from the array in RGB format.

    Apply this colour to the background colour of the layer.

    My problem is stored strings of RGB values in an array don't seem to work when I randomly pull one out and try and apply it using the above code. I don't want random colours, I want to randomly choose colours I've chosen that work well with my game and apply one.

  • What you can do if you add it to a function is simple call the function with a random number. And based on that it will change the color.

    Input: Random number from 1 to 5

    On Function Change_Color

    If Function.parameter(0) = 1

    Set background color to Red

    If function.parameter(0) = 2

    Set background color to Blue

    etc.

    When you call the function you simply write.

    Call Change_color

    Parameter (0): int(random(5)+1)

    If you are not sure how to use functions, here is a small guide I wrote, They are fairly easy to use and a absolute must to learn if you want to make anything decent in my opinion - https://www.scirra.com/forum/viewtopic.php?t=97305&p=743528#p743528

  • Can you try adding "rgb(255,255,255)" to the array, instead of just "255,255,255"?

    Not sure if it works, but give it a try

    Edit

    Another thing worth mentioning, is what rgb(r,g,b) actually does. It's just a calculation:

    r + (g * 256) + (b * 256 * 256)

    So for instance, rgb(255,255,255) = 16777215

    So you can actually put 16777215 in your array and that's equal to rgb(255,255,255) which is white

  • What you can do if you add it to a function is simple call the function with a random number. And based on that it will change the color.

    Input: Random number from 1 to 5

    On Function Change_Color

    If Function.parameter(0) = 1

    Set background color to Red

    If function.parameter(0) = 2

    Set background color to Blue

    etc.

    When you call the function you simply write.

    Call Change_color

    Parameter (0): int(random(5)+1)

    If you are not sure how to use functions, here is a small guide I wrote, They are fairly easy to use and a absolute must to learn if you want to make anything decent in my opinion - https://www.scirra.com/forum/viewtopic.php?t=97305&p=743528#p743528

    Brilliant. Seems using a function is all that was needed, does the job well. Thank you very much for your help and to everyone else who answered.

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