【问题标题】:Scores using Filestream (AS3 - Air for Android)使用 Filestream 得分 (AS3 - Air for Android)
【发布时间】:2015-01-05 10:01:38
【问题描述】:

我已经开始为 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。 如果你们知道出了什么问题(或者有其他方法可以实现我的目标),我会全力以赴。 谢谢。

【问题讨论】:

  • 您的代码看起来不错。您能否使用问题中提到的代码生成一个 apk 并共享它以进行测试,可能是问题来自您的 android 设备而不是代码?
  • 感谢您的回答。 dropbox.com/s/79k7umu5bjufxu4/Untitled-1.apk?dl=0 。我打算在另一台设备上试试看。 :)
  • 它对我来说没有问题,看看here。我使用的是 Android 4.1.1 和 AIR 15 的平板电脑。
  • 刚用我哥哥的手机试了一下,也可以。不过仍然不在我的身上。可能是我的手机,有机会我会尝试使用其他设备。感谢您的宝贵时间!

标签: android actionscript-3 air filestream


【解决方案1】:

可能来自 wonGames=score1.value 的“价值”;不存在并且 FP 将其评估为未定义并尝试将其分配给 int (wonGames) 返回 0; 在 WriteObject 中尝试:

  fileStream.writeInt(score1);

和 readObject 尝试:

  wonGames =  fileStream.readInt(); 

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

【讨论】:

  • 感谢您的回答。我尝试了您的建议,但 Flash CC 告诉我存在以下问题: 1118:将静态类型 Object 的值隐式强制转换为可能不相关的 int 类型。 (对象和整数)。
  • 在哪一行?你评论过:wonGames=score1.value;//更新变量?
猜你喜欢
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多