Search code examples
actionscript-3randomflash-cs6flashdevelop

How to loop through frame number and if true then ignore that frame number?


Hey guys so I am struggling the best way to approach this situation.

So I have 5 frames in my ovenScreen.mcChar movie clip. Each is a character that the player can unlock. If the player has enough coins then they can go to the prize screen to get a random unlockable character.

Here is how it is setup so far:

private function getPrizeHandler(e:MouseEvent):void 
    {
        //Check if prize is locked or unlocked then unlock item/ loop through random number frames
        frameLoop = randomNumber(1, 5);

        ovenScreen.mcChar.gotoAndStop(frameLoop);


        if (frameLoop == 1 && !sharedObjectCharacters.data.sharedHotDog)
        {
            sharedHotDog = true;
            sharedObjectCharacters.data.sharedHotDog = sharedHotDog;
            sharedObjectCharacters.flush ();
        }else 
        if (frameLoop == 2 && !sharedObjectCharacters.data.sharedTaco)
        {
            sharedTaco = true;
            sharedObjectCharacters.data.sharedTaco = sharedTaco;
            sharedObjectCharacters.flush ();
        }else 
        if (frameLoop == 3 && !sharedObjectCharacters.data.sharedDonut)
        {
            sharedDonut = true;
            sharedObjectCharacters.data.sharedDonut = sharedDonut;
            sharedObjectCharacters.flush ();
        }else 
        if (frameLoop == 4 && !sharedObjectCharacters.data.sharedCoffee)
        {
            sharedCoffee = true;
            sharedObjectCharacters.data.sharedCoffee = sharedCoffee;
            sharedObjectCharacters.flush ();
        }else
        if (frameLoop == 5 && !sharedObjectCharacters.data.sharedPancakes)
        {
            sharedPancakes = true;
            sharedObjectCharacters.data.sharedPancakes = sharedPancakes;
            sharedObjectCharacters.flush ();
        }

        ////////////////////////////////////////
        ovenScreen.gotoAndPlay(2); //play animations
        TweenLite.delayedCall(3.5, prizeConfettie);
        ovenScreen.removeEventListener(MouseEvent.CLICK, getPrizeHandler);


    }

As you can see I have the var frameLoop which is a random number from 1 - 5. So the character unlocked will be random and show the random unlocked character. I use the if statements to check if the random number lands on that certain frame and its not the case that it is unlocked then unlock it and save the data.

Now this all works fine but How could I go about fixing it to where if the Item is already unlocked to sort through a different frame number. So if the frameLoop lands on 2 and that character is already unlocked then repeat the random frame number until it lands on a locked character. I was thinking of setting up an array of numbers maybe that approach might be a logical one but not sure how to go about doing so.

Any help would be appreciated thanks.

Additional Info on the shared object Booleans:

private function allSharedObjectBooleans():void 
    {

        sharedObjectCharacters = SharedObject.getLocal("Characters");

        sharedHotDog = sharedObjectCharacters.data.sharedHotDog != null ? sharedObjectCharacters.data.sharedHotDog : false;
        sharedTaco = sharedObjectCharacters.data.sharedTaco != null ? sharedObjectCharacters.data.sharedTaco : false;
        sharedDonut = sharedObjectCharacters.data.sharedDonut != null ? sharedObjectCharacters.data.sharedDonut : false;
        sharedCoffee = sharedObjectCharacters.data.sharedCoffee != null ? sharedObjectCharacters.data.sharedCoffee : false;
        sharedPancakes = sharedObjectCharacters.data.sharedPancakes != null ? sharedObjectCharacters.data.sharedPancakes : false;

    }

and I create them like so:

//shared Booleans
    private var sharedHotDog:Boolean;
    private var sharedTaco:Boolean;
    private var sharedDonut:Boolean;
    private var sharedCoffee:Boolean;
    private var sharedPancakes:Boolean;

Solution

  • If the character is already unlocked, you increment the variable. If it's overboard (greater than the number of unlockable entities), set it to 1. If it looped through all characters and they all are already unlocked, do something else. And you already have an array of sorts for this, it's just located in sharedObjectCharacters.data and its indexes are string, not int. But this can be remedied by providing a map from int to string, and using this[string] syntax to check for properties. An example:

    const strUnlockables:Array=["Noone","sharedHotDog","sharedTaco","sharedDonut","sharedCoffee","sharedPancakes"];
    const unlockables:int=5; 
    private function getPrizeHandler(e:MouseEvent):void {
        var f:int=randomNumber(1,5); //frameLoop
        for (var i:int=0;i<unlockables;i++) { // check all unlockables, starting with f'th
            if (sharedObjectCharacters.data[strUnlockables[f]]===false) {
                // this "f" is the one to unlock
                sharedObjectCharacters.data[strUnlockables[f]]=true;
                sharedObjectCharacters.flush();
                ovenScreen.mcChar.gotoAndStop(f);
                ovenScreen.gotoAndPlay(2); //play animations
                TweenLite.delayedCall(3.5, prizeConfettie);
                break;
            }
            f++;
            if (f>unlockables) f=1; // loop around
        }
        // if we're here, we either did a break, or have all characters unlocked
        ovenScreen.removeEventListener(MouseEvent.CLICK, getPrizeHandler); // clean up
    }