2

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;
Nathan
  • 536
  • 4
  • 21

1 Answers1

2

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
}
Vesper
  • 18,599
  • 6
  • 39
  • 61
  • Hey Vesper thank you so much for this and the explanation. I am going to play around with it right now and see if I can get it to work. Ill let you know If I run into any problems if thats okay. Thanks. One question so the strUnlockables:Array are holding my sharedObject Booleans in them correct? – Nathan Aug 03 '16 at 17:07
  • The issue I am having is when I play the if statement is never considered/ I added a trace inside of the if statement and it is never called on. Not sure what could be the issue. The sharedHotDog, sharedTaco, etc... are all booleans that I set to false. any Ideas what could be going on? – Nathan Aug 03 '16 at 17:27
  • `strUnlockables` only holds strings, the *names* of those booleans in your shared object. Please check if you didn't make a mistake in writing those strings anywhere (I could). As a second possible reason, the `sharedObjectCharacters` isn't accessible in this code. Please do `trace(f,strUnlockables[f],sharedObjectCharacters.data[strUnlockables[f]])` just before the if statement to check if all data is appearing while iterating. – Vesper Aug 04 '16 at 03:57
  • Okay this is what I am getting back in the trace: 5 sharedPancakes undefined 1 sharedHotDog undefined 2 sharedTaco undefined 3 sharedDonut undefined 4 sharedCoffee undefined – Nathan Aug 04 '16 at 07:53
  • So maybe I did make a mistake in writting the strings elsewhere. I will keep checking for the mistake. I will update my question with more info on how I set up the shared object booleans. If you can take a look and see if I am doing something wrong above I would really appreciate it Vesper. Your the man. – Nathan Aug 04 '16 at 07:55
  • You should re-initialize your shared object's booleans right away after you read the file. See, you start with no data in shared object, and if you read "undefined", you've got inconsistent data between what's in memory and what's in SO. So, just assign `sharedObjectCharacters.data.sharedHotDog = sharedHotDog; ... sharedObjectCharacters.flush()` right in `allSharedObjectBooleans`. – Vesper Aug 04 '16 at 13:23
  • Oh! okay. That makes perfect sense. Thanks Vesper. Ill Check it out later today but pretty sure that is the reason I am getting undefined. – Nathan Aug 04 '16 at 16:10
  • Okay that was the problem for sure. Thanks a lot Vesper I appreciate your time. – Nathan Aug 04 '16 at 21:09