stowbee
stowbee

Reputation: 59

as3 change from random element in array to next element

I have an array of skin tone hex codes. At the moment, when I click a button, it randomly gets an element from the array. I'd like it to just get the next available element and if there are no more, loop back to the 1st element. Here's what I have at the moment.

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

And the button code:

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

        }

Any help would be great. Also, if it can be simplified, please let me know. This is my 1st AS3 project.

Upvotes: 1

Views: 85

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

This will take the passed array, and return a new array with a random seed (starting element) and keep the rest of the order the same. I renamed the parameters to better clarify what I interpreted them to be.

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

EDIT After some comments, I believe this is what you'd like to do:

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

    }

Upvotes: 1

Related Questions