Random colour within a list of pre-defined colours

拈花ヽ惹草 提交于 2019-12-12 01:33:45

问题


Here is my actionscript that edited from 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);
}

The colour of the balls are totally random and I was wondering if it was possible to make it so the colours would be random but from a pre-defined list of colours.

Thanks a lot.


回答1:


You can easily accomplish that by using an array of colors:

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

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

The above code will select randomly from red, green and blue. You can add new colors to the randomization by simply adding their hex code to the array.




回答2:


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];
}


来源:https://stackoverflow.com/questions/14198940/random-colour-within-a-list-of-pre-defined-colours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!