1

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.

3
  • You want the first iteration to be random and the remaining iterations sequential? Commented May 30, 2014 at 15:33
  • In the button code, "i" is not set to anything, but it is still referenced later at "myTone[i]". Is there some code missing? Commented Jun 1, 2014 at 2:57
  • I have removed that now thanks Gigggas. It's still bringing back random hex codes with a lot of repeats. Any ideas? Commented Jun 10, 2014 at 16:00

1 Answer 1

1

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

    }
Sign up to request clarification or add additional context in comments.

10 Comments

This doesn't seem to work... Each time I click the button, the hex still seems to be random and I'm getting a lot of repeats... Here's the trace output 0x8b6947 0x8b6947 0x8b6947 0xf2ca8d 0x754c24 0x754c24 0xebae7f 0x754c24 0xf2ca8d 0xf2ca8d 0xf4d1b7 0xf2ca8d 0xf2ca8d 0x754c24 0xf4d1b7 0x754c24 0x754c24 0xf2ca8d 0xf2ca8d 0xf4d1b7 0x754c24
It does work (I've tested it), you're issue must lie elsewhere. On your button click event that is posted, you're only asking for one result? Post the full code you're actually using and I can help further. If you want to see my method in action, I made a fiddle: jsfiddle.net/9wEhN/1 (Javascript and AS3 work exactly the same in this instance)
Hi. Yes, I simply want to have an array of hexs, and each time the button is clicked, it outputs next one (single) and then loops back to the first if it gets to the end. It doesn't seem to give me enough characters to insert my button code.
I've edited the code that I'm using in the orig post.
I think I understand now, you only want to randomize one time at load and then after that always give you the next tone?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.