random color

readerboy7

  • Posts: 59
i want a random color where red is 0, green is random number between 1 and 20, blue is a random number between 200 and 255. i know this is possible with code, but i don't know the code.

Alexin

  • *
  • Posts: 3127
Red, Green and Blue are hidden Number attributes.
_RandomColor is the internal name of a Color attribute. I created it as Random Color.

This is the code that goes inside the code block.
Code: [Select]
_RandomColor = Util.RGBToHex(_Red, _Green, _Blue);

Use the four blocks in the picture whenever you want to randomize the color.
"Find the fun"
alexin@stencyl.com

readerboy7

  • Posts: 59
sorry. i tried it with this behavior [see attachment] but it didn't work.
apparently color,r,g and b are undefined.

coleislazy

  • *
  • Posts: 2607
Make sure you're putting the underscore before the variables (_color, _r, etc). I can't tell if you are, since the single-line code block cuts off the bottom of the characters.

Alexin

  • *
  • Posts: 3127
What coleislazy said. Also, you aren't using the random color anywhere in the behavior.


I uploaded a behavior to demonstrate the code. It's on Forge and named Draw Rect With Random Color.
It picks a totally random color to make the effect more apparent.

(The values you want to use barely affect the final color, which always looks pure blue.)
"Find the fun"
alexin@stencyl.com

aprotasenya

  • Posts: 10
Alexin,
Thanks a lot for this code! I thought it should be done in some numerical way, but had no chance to write the code myself. This is simple and yet works great!