内pre-定义的颜色列表中随机颜色 [英] Random colour within a list of pre-defined colours

查看:112
本文介绍了内pre-定义的颜色列表中随机颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的动作,从 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);
}

球的颜色是完全随机的,我想知道是否有可能使这样的颜色是随机的,但是从颜​​色pre定义的列表。

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.

多谢了。

推荐答案

您可以轻松地完成,通过使用一个颜色数组:

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

以上code会随机由红色,绿色和蓝色选择。你可以通过简单地增加他们的十六进制code在阵列中添加新的颜色,以随​​机化。

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.

这篇关于内pre-定义的颜色列表中随机颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆