0

I have this code in my simple flash. I want to save name and score in my quiz. My code reference in this website http://www.mollyjameson.com/blog/local-flash-game-leaderboard-tutorial/

I want to make my code in just one actionscript. But I didn't success do it.

var m_FlashCookie = SharedObject.getLocal("LeaderboardExample");

var EntryName:String ="nama kamu";
var EntryScore:String ="nilai";
const NUM_SCORES_SAVED:int = 10;

inputBoxScore.text = EntryScore;
inputBoxName.text = EntryName

var latest_score_object:Object = {
  name: EntryName,
  score: EntryScore
};

var arr:Array;
arr = m_FlashCookie.data.storedArray

if ( arr == null)
{
    arr = new Array();
}

arr.push( latest_score_object );
arr.sortOn("score", Array.NUMERIC | Array.DESCENDING);

if ( arr.length < NUM_SCORES_SAVED )
{
  arr.pop();
}

btnSave.addEventListener(MouseEvent.CLICK, saveData);
function saveData(event:Event):void
{
      m_FlashCookie.data.arr = arr;
      m_FlashCookie.flush();
}

var myHTMLL:String = "";
var total_stored_scores:int = arr.length;

btnLoad.addEventListener(MouseEvent.CLICK, loadData);
function loadData(event:Event):void
{
    for (var i:int = 0; i < total_stored_scores; ++i)
    {
      // loop through every entry, every entry has a "name" and "score" field as that's what we save.
      var leaderboard_entry:Object = arr[i];

      // is this the last score that was just entered last gamestate?
      if ( leaderboard_entry == latest_score_object )
      {
        myHTMLL += (i+1) + ". <b><font color=\"#0002E5\">"+ leaderboard_entry.name + " " + leaderboard_entry.score +"</font></b><br>";
      }
      else
      {
        myHTMLL += (i+1) + ". "+ leaderboard_entry.name + " " + leaderboard_entry.score +"<br>";
      }
    }
    myHTML.text = myHTMLL;
}

Can anybody help me?

ayu
  • 23
  • 4

1 Answers1

0

You're saving the array as data.arr but reading the array as data.storedArray. You need to make them the same.

In other words, you've written this:

m_FlashCookie.data.arr = arr;

And when you load:

arr = m_FlashCookie.data.storedArray;

This clearly doesn't make sense: data.storedArray is never set, so it will never have a value, so you will always end up with a new empty array.

You need to use the same property on the shared object data. For example:

m_FlashCookie.data.storedArray = arr;
m_FlashCookie.flush();

Looking at your code, there's a number of other issues:

  1. The latest score is immediately removed because arr.length < NUM_SAVED_SCORES is going to be true from the start, since arr starts out empty, and it then calls arr.pop() which will remove the latest entry that was just added. So the array is always empty.

  2. It adds the score immediately with arr.push(latest_score_object) instead of waiting until the user clicks save, so the value of the input texts don't matter at all -- the saved values will always be "nama kamu" and "nilai".

The following fixes all the issues mentioned:

var leaderboard = SharedObject.getLocal("leaderboard");

const MAX_SAVED_SCORES:int = 10;

inputBoxName.text = "nama kamu";
inputBoxScore.text = "nilai";

var entries:Array = leaderboard.data.entries || [];

var latestEntry:Object;

displayScores();

btnLoad.addEventListener(MouseEvent.CLICK, loadClick);
btnSave.addEventListener(MouseEvent.CLICK, saveClick);

function loadClick(e:MouseEvent):void {
    displayScores();
}

function saveClick(e:MouseEvent):void {
    saveLatestScore();
    displayScores();
}

function saveLatestScore():void {

    // create the latest entry based on input texts
    latestEntry = {
      name: inputBoxName.text,
      score: inputBoxScore.text
    };

    // add the entry and sort by score, highest to lowest
    entries.push(latestEntry);
    entries.sortOn("score", Array.NUMERIC | Array.DESCENDING);

    // if reached the limit, remove lowest score
    if (entries.length > MAX_SAVED_SCORES) {
      entries.pop();
    }

    // store new sorted entries to shared object
    leaderboard.data.entries = entries;
    leaderboard.flush();
}

function displayScores():void {
    var myHTMLL:String = "";

    for (var i:int = 0; i < entries.length; ++i) {
      // loop through every entry, every entry has a "name" and "score" field as that's what we save.
      var entry:Object = entries[i];

      // is this the last score that was just entered last gamestate?
      if (entry == latestEntry)
        myHTMLL += (i+1) + ". <b><font color=\"#0002E5\">"+ entry.name + " " + entry.score +"</font></b><br/>";
      else
        myHTMLL += (i+1) + ". "+ entry.name + " " + entry.score +"<br/>";
    }
    myHTML.htmlText = myHTMLL;
}
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103