0

这是我从http://circlecube.com/2009/02/random-movement-brownian-revisited-for-as3/编辑的动作脚本:

//number of balls
var numBalls:uint = 50;
var defaultBallSize:uint = 8;

//init
makeDots();

function makeDots():void {
//create desired number of balls
for (var ballNum:uint=0; ballNum<numBalls; ballNum++){
    var c1:Number = randomColor();
    var c2:Number = randomColor();

    //create ball
    var thisBall:MovieClip = new MovieClip();
    thisBall.graphics.beginFill(c1);
    //thisBall.graphics.lineStyle(defaultBallSize, 0);
    thisBall.graphics.drawCircle(defaultBallSize, defaultBallSize, defaultBallSize);
    thisBall.graphics.endFill();

    addChild(thisBall);

    //coordinates
    thisBall.x = Math.random() * stage.stageWidth;
    thisBall.y = Math.random() * stage.stageHeight;
    //percieved depth
    //thisBall.ballNum = ballNum;
   // thisBall.depth = ballNum/numBalls;
    //thisBall.scaleY = thisBall.scaleX = thisBall.alpha = ballNum/numBalls;
    //velocity
    thisBall.vx = 0;
    thisBall.vy = 0;
    thisBall.vz = 0;

    //ball animation
    thisBall.addEventListener(Event.ENTER_FRAME, animateBall);
}
}

var dampen:Number = 0.95;
var maxScale:Number = 1.3;
var minScale:Number = .3;
var maxAlpha:Number = 1.3;
var minAlpha:Number = .3;
function animateBall(e:Event):void{
var thisBall:Object = e.target;
//apply randomness to velocity
thisBall.vx += Math.random() * 0.2 - 0.1;
thisBall.vy += Math.random() * 0.2 - 0.1;
thisBall.vz += Math.random() * 0.002 - 0.001;

thisBall.x += thisBall.vx;
thisBall.y += thisBall.vy;
//thisBall.scaleX = thisBall.scaleY += thisBall.vz;
//thisBall.alpha += thisBall.vz;
thisBall.vx *= dampen;
thisBall.vy *= dampen;
thisBall.vz *= dampen;

if(thisBall.x > stage.stageWidth) {
    thisBall.x = 0 - thisBall.width;
}
else if(thisBall.x < 0 - thisBall.width) {
    thisBall.x = stage.stageWidth;
}
if(thisBall.y > stage.stageHeight) {
    thisBall.y = 0 - thisBall.height;
}
else if(thisBall.y < 0 - thisBall.height) {
    thisBall.y = stage.stageHeight;
}

if (thisBall.scaleX > maxScale){
    thisBall.scaleX = thisBall.scaleY = maxScale;
}
else if (thisBall.scaleX < minScale){
    thisBall.scaleX = thisBall.scaleY = minScale;
}
if (thisBall.alpha > maxAlpha){
    thisBall.alpha = maxAlpha;
}
else if (thisBall.alpha < minAlpha){
    thisBall.alpha = minAlpha;
}
}

function randomColor():Number{
return Math.floor(Math.random() * 16777215);
}

球的颜色是完全随机的,我想知道是否有可能使颜色是随机的,但来自预定义的颜色列表。

非常感谢。

4

2 回答 2

4

您可以通过使用一组颜色轻松完成此操作:

var colors:Array = [0xFF0000, 0x00FF00, 0x0000FF];

function randomColor():uint
{
    return colors[int(Math.random()*colors.length)];
}

上面的代码将从红色、绿色和蓝色中随机选择。您可以通过简单地将它们的十六进制代码添加到数组中来将新颜色添加到随机化中。

于 2013-01-07T15:36:28.810 回答
1
function randomColor():Number{
    var my_clrs_arr:Array = 
        new Array(0xFFCC00,0xFF0000, 0xFFFF00, 0xFFFFFF, 0xCCCC00, 0xFACC00);
    var random_num = Math.floor(Math.random() * my_clrs_arr.lemgth);
    return  my_clrs_arr[random_num];
}
于 2013-01-07T15:37:45.460 回答