Problem getting a random gradient fill to work

Any ideas?

This works with having normal gradient fill with color:
[AS]
color_red = 0xFF0000;
color_blue = 0x0000FF;
_root.gear_x_width = Math.floor(Math.random()60)+10;
_root.gear_y_width = Math.floor(Math.random()20+_root.gear_x_width;
_root.gear_num_spokes = Math.floor(Math.random()5)+6;
_root.gear_inside = Math.floor(Math.random()
_root.gear_x_width/8)+_root.gear_x_width/6;
_root.gear_outside = Math.floor(Math.random()
_root.gear_x_width/10)+_root.gear_x_width/10;
//
with (_root[“gear”+1+i]) {
lineStyle(1, color_red, 100);
colors = [color_blue, color_red];
alphas = [100, 100];
ratios = [0, 255];
matrix = {a:0, b:_root.gear_y_width
1.5, c:0, d:_root.gear_x_width*1.5, e:0, f:0, g:0, h:0, i:1};
beginGradientFill(“radial”, colors, alphas, ratios, matrix);
draw_gear(0, 0, gear_x_width, gear_y_width, gear_num_spokes, gear_inside, gear_outside);
endFill();
}
[/AS]

This code just gives me black inside, but the fill color on the outside line is random.


_root.gear_x_width = Math.floor(Math.random()*60)+10;
_root.gear_y_width = Math.floor(Math.random()*20)+_root.gear_x_width;
_root.gear_num_spokes = Math.floor(Math.random()*5)+6;
_root.gear_inside = Math.floor(Math.random()*_root.gear_x_width/8)+_root.gear_x_width/6;
_root.gear_outside = Math.floor(Math.random()*_root.gear_x_width/10)+_root.gear_x_width/10;
//
with (_root["gear"+1+i]) {
	fill_color_a = "0x"+(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16);
	fill_color_b = "0x"+(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16);
	lineStyle(1, fill_color_a, 100);
	colors = [fill_color_b, fill_color_a];
	alphas = [100, 100];
	ratios = [0, 255];
	matrix = {a:0, b:_root.gear_y_width*1.5, c:0, d:_root.gear_x_width*1.5, e:0, f:0, g:0, h:0, i:1};
	beginGradientFill("radial", colors, alphas, ratios, matrix);
	draw_gear(0, 0, gear_x_width, gear_y_width, gear_num_spokes, gear_inside, gear_outside);
	endFill();
}

Any one? Why does the random color var not work, but a color var works?

Although the code seems to be fine… try this:

fill_color_a = Math.floor(Math.random()*255)+1 << 16 | Math.floor(Math.random()*255)+1 << 8 | Math.floor(Math.random()*255)+1;
fill_color_b = Math.floor(Math.random()*255)+1 << 16 | Math.floor(Math.random()*255)+1 << 8 | Math.floor(Math.random()*255)+1;

And it would be easier for us if you could provide your FLA. :slight_smile:

Wonderful!!! That fixed it!

So the << 16 moves the binary over 16 so the next group of numbers does not over write it? Then the | is or, so kind of simular to the add (+)?

Probably not the best definition :confused:

Thanks again!

Here is the draw gear code. If you want still want the flash file, I’m sure I can upload it. :slight_smile:

[AS]
// Draw Gear Funciton
MovieClip.prototype.draw_gear = function(x, y, x_width, y_width, num_spokes, inside, outside) {
// Keep the number of spokes greater then 4
if (num_spokes<4) {
num_spokes = 4;
}
// Set the add angle by the number of spokes
angle_add = 360/Math.round(num_spokes)/2;
// Control point time number
a = Math.tan(angle_add/4Math.PI/180);
// Set outside gear width
outside_width_x = x_width/2+outside;
outside_width_y = y_width/2+outside;
// Set start point
this.moveTo(x+x_width/2, y);
// Step threw the angle to draw outside spokes
for (angle=angle_add; angle<=360; angle += angle_add) {
x_start_point = x+x_width/2
Math.cos(angleMath.PI/180);
y_start_point = y+y_width/2
Math.sin(angleMath.PI/180);
x_control_point = x_start_point+x_width/2
aMath.cos((angle-90)Math.PI/180);
y_control_point = y_start_point+y_width/2
a
Math.sin((angle-90)Math.PI/180);
// Add a curve
this.curveTo(x_control_point, y_control_point, x_start_point, y_start_point);
// Add a spoke
x_start_point = x+outside_width_x
Math.cos((angle+angle_add/4)Math.PI/180);
y_start_point = y+outside_width_y
Math.sin((angle+angle_add/4)Math.PI/180);
this.lineTo(x_start_point, y_start_point);
x_start_point = x+outside_width_x
Math.cos((angle+angle_add3/4)Math.PI/180);
y_start_point = y+outside_width_y
Math.sin((angle+angle_add
3/4)Math.PI/180);
this.lineTo(x_start_point, y_start_point);
x_start_point = x+x_width/2
Math.cos((angle+angle_add)Math.PI/180);
y_start_point = y+y_width/2
Math.sin((angle+angle_add)Math.PI/180);
this.lineTo(x_start_point, y_start_point);
// Add to the angle
angle += angle_add;
}
//
// Control point time number
a = Math.tan(22.5
Math.PI/180);
// Set inside gear width
inside_width_x = x_width/2-inside;
inside_width_y = y_width/2-inside;
// Set start point
this.moveTo(x+inside_width_x, y);
// Step threw the angle to draw inside
for (angle=45; angle<=360; angle += 45) {
x_start_point = x+inside_width_xMath.cos(angleMath.PI/180);
y_start_point = y+inside_width_yMath.sin(angleMath.PI/180);
x_control_point = x_start_point+inside_width_xaMath.cos((angle-90)Math.PI/180);
y_control_point = y_start_point+inside_width_y
a*Math.sin((angle-90)*Math.PI/180);
// Add a curve
this.curveTo(x_control_point, y_control_point, x_start_point, y_start_point);
}
//
};
//
[/AS]

I’ve seen a really good explanation about the bitwise stuff, let me find the link for you. Meantime, look up the bitwise OR and bitwise left shift operators in the AS Dictionary (if you haven’t done so already). :wink:

And you’re welcome. :beam: