【发布时间】:2014-04-10 01:23:43
【问题描述】:
我目前正在使用 LibGDX 创建的游戏具有“高分”功能,其中高分保存在文本文件中,然后写入或读取到屏幕上。我在这里遇到很多错误。我尝试设置权限以允许外部文件写入,我尝试将我的 .txt 文件放在不同的目录中。我尝试使用 Gdx.files.internal() and Gdx.files.external() 没有任何效果。这是我获得和设置高分的两种方法。
private int getHighScore()
{
FileHandle scoreFile = Gdx.files.local("data/high_score.txt");
String text = scoreFile.readString();
int highScore = Integer.parseInt(text);
return highScore;
}
private void setHighScore(int newScore)
{
FileHandle scoreFile = Gdx.files.local("data/high_score.txt");
String score = Integer.toString(newScore);
scoreFile.writeString(score, false);
}
在游戏中死亡(应该显示分数时),我从 LibGDX 收到此错误:
03-06 22:31:37.437: ERROR/AndroidRuntime(14533): FATAL EXCEPTION: GLThread 4617
com.badlogic.gdx.utils.GdxRuntimeException: File not found: /data/data/hasherr.floppyfish.android.core/files/data/high_score.txt (Local)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:133)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:198)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:186)
at hasherr.ghostly.main.state.DeathState.getHighScore(DeathState.java:122)
at hasherr.ghostly.main.state.DeathState.render(DeathState.java:87)
at hasherr.ghostly.main.state.StateManager.render(StateManager.java:37)
请注意:
找不到文件:/data/data/hasherr.floppyfish.android.core/files/data/high_score.txt(本地)
/data/data/hasherr.floppyfish.android.core/files/data/high_score.txt 不是我游戏中的目录/文件。 data/high_score.txt 是,这就是我想要访问的文件。我对Gdx.files.local(path) 方法不是很熟悉,所以我不确定它是从哪里获取的。
如何让我的程序使用这个文本文件而不抛出错误?
【问题讨论】:
-
你是否在每个渲染周期中调用
getHighScore?如果是这样,您应该更改并存储它,而不是一直从文件中获取它。
标签: java android file file-io libgdx