2 Replies Latest reply on Apr 24, 2006 7:11 AM by Nalinda Bandara

    shared Object for high score

    Nalinda Bandara Level 1
      I am using shared Object for saving high score on my game,
      But its not working properly, not adding high score at right place
      Please check the codes, i cant find the eror

      for the 1st frame

      var local:SharedObject = SharedObject.getLocal("name");
      var local:SharedObject = SharedObject.getLocal("high");

      name1 = local.data.name1;
      name2 = local.data.name2;
      name3 = local.data.name3;
      name4 = local.data.name4;
      name5 = local.data.name5;
      high1 = local.data.high1;
      high2 = local.data.high2;
      high3 = local.data.high3;
      high4 = local.data.high4;
      high5 = local.data.high5;

      for(i=1;i<6;i++) {
      if(_root["high"+ i]== undefine){
      _root["high"+ i] = 00000;
      _root["name"+ i] = "-------------------";
      }
      }


      //after add the score, when user ad his name, check is it a high score and add it

      on(keyPress "<Enter>") {
      for(i=0;i<6;i++) {
      if(_root["high"+ i] < currentScore){
      // trace(i);

      local.data["name" + (i+1)] = local.data["name" + i];
      local.data["high" + (i+1)] = local.data["high" + i];

      local.data["name" + i] = _root.name;
      local.data["high" + i] = _root.currentScore;
      trace(i);

      break;
      }
      }

      }

      Please help
        • 1. Re: shared Object for high score
          Level 7
          NutsyNalinda,

          > I am using shared Object for saving high score on my
          > game, But its not working properly, not adding high score
          > at right place

          My guess is that it's working exactly right, but that you've told it to
          do something that doesn't accomplish your goal.

          > for the 1st frame
          >
          > var local:SharedObject = SharedObject.getLocal("name");
          > var local:SharedObject = SharedObject.getLocal("high");

          Right off, this is a problem. In the first line, you're declaring a
          variable, local, as an instance of SharedObject. This instance contains
          whatever data is stored in the "name" persistent object. In the second
          line, you obliterate the first variable by declaring a new one in its place,
          containing whatever data is stored in the "high" persistent object. At this
          point, the "name" data is no longer known to your SWF. What follows, then,
          is that these ...

          > name1 = local.data.name1;
          > name2 = local.data.name2;
          > name3 = local.data.name3;
          > name4 = local.data.name4;
          > name5 = local.data.name5;

          ... will all be undefined, because the local variable no longer has any idea
          what the "name" data are.

          Make sense?

          Honestly, it makes more sense to combine these data into a single
          persistent object. Perhaps something named "NutsyNalindaGame". If you
          stored your name-and-value pairs in an object, and stored several objects in
          an array, you could organize everything neatly.

          e.g.
          var highScores:Array = new Array();
          highScores.push(new Object(name:"Nutsy", score:1000000));
          highScores.push(new Object(name:"Nalinda", score:999999));
          highScores.push(new Object(name:"David", score:999998));

          In this way, while inside the game, you'll have a highScores object (an
          array) that contains three (or however many) generic objects that each have
          a name and score property. To retrieve the score of entry number three, you
          would check ...

          highScores[2].score

          ... since arrays start at zero (that makes array[2] provide the third item).

          To retrieve the name of entry number one, you should check ...

          highScores[0].name

          ... all nice and neat. In face, you could simply drop this highScores
          object into your SharedObject instance, so that you wouldn't have to use so
          many stray variables, like this ...

          > high1 = local.data.high1;
          > high2 = local.data.high2;
          > high3 = local.data.high3;
          > high4 = local.data.high4;
          > high5 = local.data.high5;

          ... not that there's anything wrong with "stray variables"; it's just that
          they're like loose socks everyhere. Organizing them into objects/arrays is
          like putting those socks into drawers.

          Just a thought. If it makes more sense to you to keep your data in two
          separate persistent objects, have at it. Let's keep looking at your
          code ...

          > for(i=1;i<6;i++) {

          Here, we're going to do something five times.

          > if(_root== undefine){

          There is no term "undefine" in ActionScript. You probably meant
          undefined, but then I wonder what you were aiming for, here. _root will
          never be undefined. _root always points to the main timeline of the
          currently active document level. So the next two lines will never happen --
          but let's look at them anyway.

          > _root = 00000;
          > _root = "-------------------";
          > }
          > }

          The first line sets _root to the number 00000 -- but I have to say,
          that's really confusing. _root refers to a timeline, which means it refers
          to a movie clip, not a number. What were you going for, here? The second
          line attempts to set _root to a string, which only gets more baffling to me.

          > //after add the score, when user ad his name, check is
          > it a high score and add it
          >
          > on(keyPress "<Enter>") {
          > for(i=0;i<6;i++) {
          > if(_root < currentScore){
          > // trace(i);

          Somewhere along the line, you just have declared a currentScore
          variable. Assuming you did, you're checking to see if _root is less than
          that value. The trouble is, _root doesn't refer to a number at all. It
          may, at this point, refer to the string "-------------------", set
          earlier -- but most likely, it will refer to the main timeline of the
          currently active document level.

          > local.data = local.data;
          > local.data = local.data;

          In this line, you're setting the previously declared local variable's
          data property to itself. then you're doing it again.

          > local.data = _root.name;
          > local.data = _root.currentScore;

          Finally, you're setting the local variable's data property to a variable
          named "name" in the _root (has this variable been declared somewhere?) and
          then you're completely erasing that data by replacing the local variable's
          data property with the value of currentScore. (Remember, in both cases,
          here, local only refers to the "high" persisent object.)

          I hope that by stepping through this, you're a bit clearer on what's
          going on. It seems like you're aiming for something -- not exactly sure
          what, yet -- but you need to think through your approach. Does this help?


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: shared Object for high score
            Nalinda Bandara Level 1
            Hi David,

            Wow that’s huge reply,
            Thanx I learn lot
            First sorry for my bad English, I am still struggling with It,
            ------------------------------
            > var local:SharedObject = SharedObject.getLocal("name");
            > var local:SharedObject = SharedObject.getLocal("high");

            It’s my mistake- using same variable for both,

            > name1 = local.data.name1;
            > name2 = local.data.name2;
            > name3 = local.data.name3;
            > name4 = local.data.name4;
            > name5 = local.data.name5;

            Here , name data is the high scorer name, and there is dynamic text field on the root called name1, name2, name3….

            For the first time name1, nqme2,…high1, high2…. will be undefined, so I used this

            for(i=1;i<6;i++) {
            if(_root["high"+ i]== undefined){
            _root["high"+ i] = 00000;
            _root["name"+ i] = "-------------------";
            }
            }
            and it’s not “if(_root== undefined)”, I know

            if the _root.high1 = = undefined the high1 goes to 0, and name1 to blank (‘---‘);
            I add 0000, if it equal to 0 I need to show it as “00000”,It’s a mistake but its not harm for the code,
            Later I’ll add if(name1 = 0) name1 = “0000”;

            It’s worked well,


            Still I struggling with some simple scripts, like push and new Objects you mention, so I used long way to do that. Anyway now I’m trying to fix this like as you said.

            var highScores:Array = new Array();
            highScores.push(new Object(name:"Nutsy", score:1000000));
            highScores.push(new Object(name:"Nalinda", score:999999));
            highScores.push(new Object(name:"David", score:999998));

            Here lets say next player “Stille” score 1000001, how should I push him to the first place? I mean to the highScores[0],




            > on(keyPress "<Enter>") {
            > for(i=0;i<6;i++) {
            > if(_root < currentScore){
            > // trace(i);

            >Somewhere along the line, you just have declared a
            currentScore variable. Assuming you did, you're
            .checking to see if _root is less than
            that value. The trouble is, _root doesn't refer to a number at all. It
            may, at this point, refer to the string "-------------------", set
            earlier -- but most likely, it will refer to the main timeline of the
            >currently active document level.

            Here its not _root, its _root[“high”+i]
            CurrentScore is the score player did, here I check that score is more than other high scores and push where it should be ( for first, second, third , fourth…)

            on(keyPress "<Enter>") {
            for(i=0;i<6;i++) {
            if(_root["high"+ i] < currentScore){

            Anyway I learned lot from your reply, Thanx for that.

            Cheers!

            nalinda