Search code examples
actionscript-3movieclipflash-cs6shared-objects

How to display Movie Clip if shared Object is equal to a number AS3


Hey everyone so what I've been trying ti accomplish with no success is how to display my Movie Clip object named achiev_10 when the nScore which is a number equal to 10.

Here is what I have so far in my Shared Object Data.

In my Constructor I have this:

//Initialize our shared object give it a path to save local data
        sharedObject = SharedObject.getLocal("GeometryBlast");

         if (sharedObject.data.highScore == null)
         {     
               // checks if there is save data
               trace("No saved data yet."); // if there isn't any data on the computer...
               sharedObject.data.highScore = nScore; // ...set the savedScore to 0
         } else 
         {
               trace("Save data found."); // if we did find data...
               loadData(); // ...load the data
         }

Then in the saveData(); function I have this:

public function saveData():void
    {
        if (nScore > sharedObject.data.highScore )
        {
            sharedObject.data.highScore = nScore;

        }
        menuEnd.bestScore.text = " " + sharedObject.data.highScore;
        sharedObject.flush();
        //trace("Data Saved!");
        //sharedObject.clear();

        if (nScore == 10)
        {
            achiev_10 = new Achievment_10();
            menuEnd.addChild(achiev_10);
            achiev_10.x = stage.stageWidth / 2;
            achiev_10.y = stage.stageHeight / 2;
            sharedObject.flush();
        }

    }

Now currently in the if (nScore == 10) where I add the Move Clip, it does work and it does display. But when i go back to check my Move Clip achievement it dissapears. I don't really know what i need to do to save the data if the save data.highscore is equal to 10 then always display that achievement.

I also tried this but nothing:

 if (sharedObject.data.highScore == 10)
    {
        achiev_10 = new Achievment_10();
        menuEnd.addChild(achiev_10);
        achiev_10.x = stage.stageWidth / 2;
        achiev_10.y = stage.stageHeight / 2;
        sharedObject.flush();
    }

please help thank you!


Solution

  • You will need several functions to work with SharedObject. But you should know, user can clear values from the SharedObject, and achievements will be lost.

    private function getScore(key:String, domain:String):* {
        try {
            var so:SharedObject = SharedObject.getLocal(domain);
            var data:Object = so.data;
    
            if (key in data) {
                return data[key];
            } else {
                trace(key + " doesn't present in SharedObject");
            }
    
        } catch (e:*) {
            trace("Oops, something goes wrong…");
        }
    }
    
    private function saveScore(value:*, key:String, domain:String):void {
        try {
            var so:SharedObject = SharedObject.getLocal(domain);
            so.data[key] = value;
            so.flush();
        } catch (e:*) {
            trace("Oops, something goes wrong…");
        }
    }
    

    Now you can work with scores:

    var domain:String = "GeometryBlast";
    var scoreKey:String = "ScoreKey";
    const newcomer: int = 10;
    
    //Getting scores
    var myScore: Number = getScore(scoreKey, domain);
    
    //Simple example how you could manage score values
    if(!isNaN(myScore)){
        //Check achievements
    
        if(myScore >= newcomer){
            //Add 'Newcomer' achievement to the screen
        }
    }
    
    //Saving scores
    var someScores:Number = 10;
    saveScore(someScores, scoreKey, domain);