A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Shared Objects: creating directories?

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    157

    Shared Objects: creating directories?

    Dear fellow geeks,

    I'm creating a video game where the user can have multiple games. Each game has a few vars, being the current level, the name of the game, high scores per level etc.

    For instance, I could have two games (the shared object is called "cookie" in this example) with this structure:

    cookie.Data{
    g1{ //game 1
    level = 2;
    high0 = 409 //high scores for levels 0-2
    high1 = 237
    high2 = 566
    HP = 61
    mana = 15
    }
    g2{ // game 2
    level = 1
    high0 = 255
    high1 = 488
    HP = 28
    mana = 44
    }
    }

    Problem: I can't seem to find the right syntax to create this structure!

    I have tried this, but it does not work:

    Actionscript Code:
    function newGame():Void{

        if(cookie.data.gameCount > 0){ // if there already are games
            cookie.data.gameCount ++;
        }
        else{ // if this is the first new game
            cookie.data.gameCount = 1;
        }
        trace(cookie.data.gameCount);       // returns the correct number :)

        var current:Number= cookie.data.gameCount;
        // 'number' corresponding with most recently created game

        cookie.data["g"+_root.current] = new Object;
        // create directory for current game

        cookie.data["g"+_root.current].lvl = new Number(0);
        // sets new game level to 0; I think the error is somewhere here, since tracing this returns undefined!

        cookie.flush();
        // saves data locally to user's computer

        trace(cookie.data.g19.lvl);
        //the 19 can be replaced by any number, as long as it is between 0 and _root.current.
        // this trace() somehow returns "undefined" ?????

        //--------
        /*so, the cookie consists of:
                cookie.data{
                    gamecount :Number;
                    g1{
                        lvl:Number
                        >room for more vars here<
                    }g2 (optional){
                        idem g1
                    }etc.
                }*/

        //--------

        loadGame(current); // you can imagine what this does ;)
    }

    Can anybody help me, please?
    thanks a bunch

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Sorry, but it's not possible to create directories inside the Shared Object

    However, you could name your items like this: g1_level = 2; g1_high0 = 409; etc.

    And then, use something like this: cookie.data._root["g"+_root.current+"_lvl"] = 2;

    Did you get my point?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Hi,

    You can have other objects stored inside objects.
    And objects stored in LSOs.
    Here's a simple example:
    Actionscript Code:
    var my_so:SharedObject = SharedObject.getLocal("someSO");

    // it's the first time
    if (my_so.data.g1 == undefined)
    {
        my_so.data.g1 = new Object(); // create the object
        my_so.data.g1.level = 5; // store a value as a propery of the object
    }
    else
    {
        // the LSO exists
        // this is the value stored in the g1 object
        trace(my_so.data.g1.level);
       
        // lets change it:
        my_so.data.g1.level++;
    }

  4. #4
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    I'd like to thank you both for your replies. Nunomira, I do not see the difference between

    cookie.data["g"+_root.current] = new Object; cookie.data["g"+_root.current].lvl = new Number(0);

    and

    my_so.data.g1 = new Object();
    my_so.data.g1.level = 5;

    Where does your magic trick come in? Is it the Number data type?

    Thank you

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    While you have: cookie.data["g"+_root.current] = new Object; which in other words is: cookie.datag1; because you do not have any dot between data and ["g"+_root.current]
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    You don't have new Object(), you have new Object.

    And
    myvar = new Number(0)
    isn't required.
    You can just have
    myvar = 0;

  7. #7
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    @ nig
    I'm pretty sure _root["john"] = _root.john

    @ nunomira
    I'll try that, thank you

  8. #8
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    koenahn,

    You're right.
    I suggest you read:
    http://goo.gl/uu7jg
    http://goo.gl/TjrUF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center