View Full Version : Random Color Fill
DanDaMan
February 22nd, 2009, 07:31 PM
How can I fill a MovieClip with a random color using Actionscript 3?
.ral:cr
February 23rd, 2009, 02:36 AM
Math.random()*0xffffff
DanDaMan
February 23rd, 2009, 08:35 PM
I tried using this.color=Math.random()*0xFFFFFF but it did not work.
JTF2
February 23rd, 2009, 10:34 PM
import flash.geom.ColorTransform;
var ct:ColorTransform = new ColorTransform();
ct.color = Math.random()* 0xffffff;
this.transform.colorTransform = ct;
BeerOclock
February 23rd, 2009, 10:41 PM
I do this, but its probably not the best way:
static public function rand_color():uint {
return uint(rand_between(0, 255) + 256*rand_between(0, 255) + 65536*rand_between(0, 255));
}
Edit: oops, didnt post my rand_between() function.
Oh well, you can figure it out ;)
bLasTamos
February 23rd, 2009, 11:38 PM
How can I fill a MovieClip with a random color using Actionscript 3?
with (yourmc.graphics) {
beginFill(Math.random()*0xFFFFFF);
drawRect(0, 0, width, height);
endFill();
}
.ral:cr
February 24th, 2009, 02:59 AM
I do this, but its probably not the best way:
static public function rand_color():uint {
return uint(rand_between(0, 255) + 256*rand_between(0, 255) + 65536*rand_between(0, 255));
}
Edit: oops, didnt post my rand_between() function.
Oh well, you can figure it out ;)
i don't think is good at all. when you have each channel value separately you combine them with bitwise operators ">>"
what you have there is a simple adding operation.
or is the uint doing something special? i'm not familiar with it.
TheCanadian
February 24th, 2009, 03:33 AM
It would still work but there is no reason to not just use 0xFFFFFF * Math.random() or 16777215 * Math.random() (which I think would be a cuny faster).
bLasTamos
February 24th, 2009, 09:36 AM
what you have there is a simple adding operation.
His code should work, he actually does the bitwise operations, only with a multiplication instead of rotation. He multiplies by 0x100 for blue and 0x10000 for red, so it works.
BeerOclock
February 24th, 2009, 01:55 PM
It definetly works, because Ive been using it.
Its just probably not as clever or fast as 0xFFFFFF * Math.random(), Which I am now using :)
I tried using this.color=Math.random()*0xFFFFFF but it did not work.
Ive never seen that kind of code for setting color. Maybe thats the problem?
Try this instead :
this.color = 0x00ff00;
is the object solid green? If that doesnt work either, then its not the random code thats the problem, its the "this.color" part.
Kasimir
February 24th, 2009, 02:11 PM
How to do this with, like random color between colors (like 5 hex colours) where it would randomize it + multiple movieclips would randomize in the same color?
DanDaMan
February 24th, 2009, 08:31 PM
Thanks a lot, guys. Works like a charm =D
Krilnon
February 24th, 2009, 09:36 PM
0xFFFFFF * Math.random() or 16777215 * Math.random() (which I think would be a cuny faster).
They compile into the same thing… so the advantage would be ridiculously minimal and only exist when compiling. The number gets put into the SWF's integer constant pool, where it's just a 32-bit integer instead of a string like it is in code, and is fetched later in bytecode with pushint.
Sorry for making a post about something like this… I have been doing something related to this recently so it was on my mind when reading your post. :P
TheCanadian
February 25th, 2009, 02:59 AM
They compile into the same thing… so the advantage would be ridiculously minimal and only exist when compiling. The number gets put into the SWF's integer constant pool, where it's just a 32-bit integer instead of a string like it is in code, and is fetched later in bytecode with pushint.
Sorry for making a post about something like this… I have been doing something related to this recently so it was on my mind when reading your post. :P
Well I did want to know, I didn't know if Flash converted at run or compile time. Although logically I should have :|
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.