[英]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.