简体   繁体   English

使用Filestream进行评分(AS3-Air for Android)

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

I've started a little board game for android and i've been researching ways to permanently save scores. 我已经开始为Android开发一个小型棋盘游戏,并且一直在研究永久保存分数的方法。 Everytime the user wins a game against the "AI", he/she gains a point (which can be used to unlock special pawns and boardgames). 每当用户赢得针对“ AI”的游戏时,他/她都会获得一个积分(可用于解锁特殊的棋子和棋盘游戏)。 I tries with SharedObject but apparently they can be erased if user delete game data (or something). 我尝试使用SharedObject,但如果用户删除游戏数据(或其他内容),显然可以删除它们。 So I went with Filestream. 所以我选择了Filestream。 I read a few posts online about it and thought I understood, but since it's not working for me, I guess I didn't :). 我在网上阅读了一些有关它的文章,并认为我可以理解,但是由于它对我不起作用,我想我没有:)。

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 is updated, but when I close the app and restart it, it is back to 0. If you guys know what's wrong (or have another way to achieve my goal), I'm all ears. wonGames已更新,但是当我关闭该应用程序并重新启动它时,它又恢复为0。如果你们知道出了什么问题(或有另一种方式实现我的目标),我就会无所适从。 Thank you. 谢谢。

Probably “value” from wonGames=score1.value; 可能是来自wonGames = score1.value的“ value”; not exists and FP evaluate it to undefined and trying to assign it to int (wonGames) return 0; 不存在,FP将其评估为undefined并尝试将其分配给int(wonGames)返回0; In WriteObject try: 在WriteObject中尝试:

  fileStream.writeInt(score1);

and readObject try: 和readObject尝试:

  wonGames =  fileStream.readInt(); 

// wonGames=score1.value;//update the variable // wonGames = score1.value; //更新变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM