as3从数组中的随机元素更改为下一个元素

时间:2014-05-30 15:19:37

标签: arrays actionscript-3

我有一系列肤色十六进制代码。目前,当我单击一个按钮时,它会从数组中随机获取一个元素。我希望只获得下一个可用元素,如果没有,则循环回到第一个元素。这就是我现在所拥有的。

var skinTones:Array = ["0xebae7f","0x754c24","0xf2ca8d","0xf4d1b7","0x8b6947"];

按钮代码:

menuMC.buttFace.addEventListener(MouseEvent.CLICK, clickFace);
        function clickFace(event:MouseEvent): void 
        {
            function getSkinTone(source, amount) {
                var i = Math.round(Math.random() * (source.length - 1)); //random seed (starting point)
                var myTone = new Array(); //the new array

                while(myTone.length < amount){
                    if (i >= source.length) i = 0;  //if the current iterator if out of bounds, reset to 0
                    myTone.push(source[i]);
                    i++;
                }
                return myTone;
            }

            var toneHex = getSkinTone(skinTones,1);
            trace(toneHex);

            TweenLite.to(faceWrapperMC.faceMC, 0.5, {colorTransform:{tint:toneHex, tintAmount:1}}); 
            TweenLite.to(faceWrapperMC.earsMC, 0.5, {colorTransform:{tint:toneHex, tintAmount:1}}); 
            TweenLite.to(faceWrapperMC.eyesMC.eyes.eyelid, 0.5, {colorTransform:{tint:toneHex, tintAmount:1}}); 

        }

任何帮助都会很棒。此外,如果可以简化,请告诉我。这是我的第一个AS3项目。

1 个答案:

答案 0 :(得分:1)

这将接受传递的数组,并返回一个带有随机种子(起始元素)的新数组,并保持顺序的其余部分相同。我重命名了这些参数,以便更好地阐明我对它的解释。

    function getSkinTone(source:Array,amount:int):Array {
        var i:int = Math.round(Math.random() * (source.length - 1)); //random seed (starting point)
        var myTone:Array = new Array(); //the new array

        while(myTone.length < amount){
            if (i >= source.length) i = 0;  //if the current iterator if out of bounds, reset to 0
            myTone.push(source[i]);
            i++;
        }
        return myTone;
    }

修改 经过一些评论后,我相信这就是你想做的事情:

var skinTones:Array = ["0xebae7f","0x754c24","0xf2ca8d","0xf4d1b7","0x8b6947"]; //these can be stored as uint instead of strings since TweenLite will just convert them to uint anyway
var skinToneIndex:int = -1; //a global var to hold the current index - starting as -1 to indicate it hasn't been set yet.

function getNextSkinTone():String {
    if(skinToneIndex == -1) skinToneIndex = Math.round(Math.random() * (skinTones.length - 1)); //if not set, make it a random starting number

    skinToneIndex++; //increment it
    if(skinToneIndex >= skinTones.length) skinToneIndex = 0; //if out of bounds reset to 0;

    return skinTones[skinToneIndex];
}

menuMC.buttFace.addEventListener(MouseEvent.CLICK, clickFace);
function clickFace(event:MouseEvent): void {

        var toneHex:uint = getNextSkinTone();
        trace(toneHex);

        TweenLite.to(faceWrapperMC.faceMC, 0.5, {colorTransform:{tint:toneHex, tintAmount:1}}); 
        TweenLite.to(faceWrapperMC.earsMC, 0.5, {colorTransform:{tint:toneHex, tintAmount:1}}); 
        TweenLite.to(faceWrapperMC.eyesMC.eyes.eyelid, 0.5, {colorTransform:{tint:toneHex, tintAmount:1}}); 

    }