簡體   English   中英

使用Filestream進行評分(AS3-Air for Android)

[英]Scores using Filestream (AS3 - Air for Android)

我已經開始為Android開發一個小型棋盤游戲,並且一直在研究永久保存分數的方法。 每當用戶贏得針對“ AI”的游戲時,他/她都會獲得一個積分(可用於解鎖特殊的棋子和棋盤游戲)。 我嘗試使用SharedObject,但如果用戶刪除游戲數據(或其他內容),顯然可以刪除它們。 所以我選擇了Filestream。 我在網上閱讀了一些有關它的文章,並認為我可以理解,但是由於它對我不起作用,我想我沒有:)。

var wonGames:int; //variable I want to save

function writeObject():void { //First function to create/write in the file
    var score1:Object = new Object();
    score1.value =  wonGames; //value of object = variable value
    trace(score1.value);

    var file:File = File.applicationStorageDirectory.resolvePath("score1.file");
    var fileStream:FileStream = new FileStream(); 
    fileStream.open(file, FileMode.WRITE);
    fileStream.writeObject(score1);
    fileStream.close();
};


function readObject():void { //second function to read the file and modify the variable.

    var file:File = File.applicationStorageDirectory.resolvePath("score1.file");
    if (!file.exists) {
        return;
    }
    var fileStream:FileStream = new FileStream();
    fileStream.open(file, FileMode.READ);
    var score1:Object = fileStream.readObject(); 
    wonGames=score1.value;//update the variable
    fileStream.close();
};

btn26.addEventListener(MouseEvent.CLICK, fnBtn26);
function fnBtn26(e: Event): void { //third function, where the winning happens
    if (winP1.currentFrame==1){ //game won
        winP1.nextFrame(); //victory screen
        readObject(); //get old value for score 1
        wonGames=wonGames+1; //update it
        trace("WonGames= "+ wonGames);
        writeObject(); //write it in file
    }
};

wonGames已更新,但是當我關閉該應用程序並重新啟動它時,它又恢復為0。如果你們知道出了什么問題(或有另一種方式實現我的目標),我就會無所適從。 謝謝。

可能是來自wonGames = score1.value的“ value”; 不存在,FP將其評估為undefined並嘗試將其分配給int(wonGames)返回0; 在WriteObject中嘗試:

  fileStream.writeInt(score1);

和readObject嘗試:

  wonGames =  fileStream.readInt(); 

// wonGames = score1.value; //更新變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM