无法在共享对象as3中保存数组

时间:2016-01-07 12:51:19

标签: arrays actionscript-3 shared-objects

我的简单flash中有这个代码。我想在测验中保存姓名和分数。我在本网站http://www.mollyjameson.com/blog/local-flash-game-leaderboard-tutorial/

中的代码参考

我想在一个动作脚本中创建我的代码。但我没有成功。

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;
}

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您将数组保存为data.arr,但将数组读取为data.storedArray。你需要让它们变得一样。

换句话说,你已经写过:

m_FlashCookie.data.arr = arr;

加载时:

arr = m_FlashCookie.data.storedArray;

这显然没有意义:data.storedArray永远不会被设置,所以它永远不会有值,所以你总是会得到一个新的空数组。

您需要在共享对象data上使用相同的属性。例如:

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

查看您的代码,还有其他一些问题:

  1. 最新分数会立即被删除,因为arr.length < NUM_SAVED_SCORES从一开始就是真的,因为arr开始为空,然后调用arr.pop(),这会删除刚刚添加的最新条目。所以数组总是空的。

  2. 它会立即使用arr.push(latest_score_object)添加分数,而不是等到用户点击保存,因此输入文本的值根本不重要 - 保存的值将始终为& #34; nama kamu&#34;和&#34; nilai&#34;。

  3. 以下修复了上述所有问题:

    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;
    }