我正在使用Libgdx在Android工作室制作一个简单的游戏,我将我的高分记录为txt文件中的一个简单整数,该文件位于android> assets文件夹中。在我的GameOverState中,我将当前分数与文本文件中的分数进行比较,如果它更大,则在txt文件中替换。 此外,每次我进入MenuState时,都会从teh文件中读取高分并显示为BitmapFont。问题是:当我在桌面上运行它时效果很好但是当我在模拟器中运行它时会发生错误:
com.badlogic.gdx.utils.GdxRuntimeException: File not found: hs.txt (Local)
这是我的GameOverState:
package com.asya.game.states;
import com.asya.game.Ozone;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameOverState extends state {
public int sc;
private Texture bgr;
private BitmapFont scoreFont;
public String txtScore;
public Integer hs;
public SaveHS saving;
public GameOverState(GameStateManager gsm, int score) {
super(gsm);
bgr = new Texture("gameOver.png");
sc = score;
scoreFont = new BitmapFont();
scoreFont.setColor(Color.WHITE);
//write to file if score is greater than the previous score
FileHandle file = Gdx.files.local("hs.txt");
txtScore = file.readString();
hs = Integer.parseInt(txtScore);
if (score > hs){
hs = score;
txtScore = Integer.toString(hs);
file.writeString(txtScore, false);
}
/*
saving = new SaveHS();
hs = saving.Load();
if (score > hs) {
hs = score;
saving.Save(score);
}
*/
cam.setToOrtho(false, Ozone.WIDTH, Ozone.HEIGHT);
}
@Override
protected void handleInput() {
if(Gdx.input.justTouched())
gsm.set(new MenuState(gsm));
}
@Override
public void update(float dt) {
handleInput();
}
@Override
public void render(SpriteBatch sb) {
sb.begin();
sb.setProjectionMatrix(cam.combined);
sb.draw(bgr, cam.position.x - (cam.viewportWidth / 2), 0);
scoreFont.draw(sb, "SCORE: " + sc, cam.position.x / 2 + 140, cam.position.y + 200);
sb.end();
}
@Override
public void dispose() {
bgr.dispose();
scoreFont.dispose();
}
}
这是我的MenuState:
package com.asya.game.states;
import com.asya.game.Ozone;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.GdxRuntimeException;
import java.io.FileNotFoundException;
import java.io.ObjectOutputStream;
public class MenuState extends state implements InputProcessor{
private Texture background;
private Texture playBtn, helpBtn;
private Vector2 posPlayBtn, posHelpBtn;
private Rectangle boundPlayBtn, boundHelpBtn;
private String highScore;
private BitmapFont hScoreFont;
public SaveHS saving;
public int s;
public MenuState(GameStateManager gsm) {
super(gsm);
background = new Texture("bgr.png");
playBtn = new Texture("play.png");
helpBtn = new Texture("help.png");
cam.setToOrtho(false, Ozone.WIDTH, Ozone.HEIGHT);
posPlayBtn = new Vector2((Ozone.WIDTH / 2)- (playBtn.getWidth()/2), Ozone.HEIGHT/2 +playBtn.getHeight()/2);
posHelpBtn = new Vector2((Ozone.WIDTH / 2) - (helpBtn.getWidth() / 2), Ozone.HEIGHT / 2 - helpBtn.getHeight() / 2);
boundPlayBtn = new Rectangle(posPlayBtn.x, posPlayBtn.y, playBtn.getWidth(), playBtn.getHeight());
boundHelpBtn = new Rectangle(posHelpBtn.x, posHelpBtn.y, helpBtn.getWidth(), helpBtn.getHeight());
hScoreFont = new BitmapFont();
hScoreFont.setColor(Color.WHITE);
FileHandle file = Gdx.files.local("hs.txt");
highScore = file.readString();
/*
saving = new SaveHS();
s = saving.Load();
*/
Gdx.input.setInputProcessor(this);
}
@Override
public void handleInput() {
}
@Override
public void update(float dt) {
handleInput();
}
@Override
public void render(SpriteBatch sb) {
//open sprite batch and close when done
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(background, cam.position.x - (cam.viewportWidth / 2), 0); //image, starting from coordinate 0, 0 (bottom left corner) and width and heiht
hScoreFont.draw(sb, "HIGH SCORE: " + highScore, cam.position.x / 2 + 140, cam.position.y + 200);
sb.draw(playBtn, posPlayBtn.x, posPlayBtn.y);
sb.draw(helpBtn, posHelpBtn.x, posHelpBtn.y );
sb.end();
}
@Override
public void dispose() {
background.dispose();
playBtn.dispose();
helpBtn.dispose();
hScoreFont.dispose();
System.out.println("Menu State Disposed");
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(boundPlayBtn.contains(Gdx.graphics.getWidth() - screenX, Gdx.graphics.getHeight() - screenY)){
gsm.set(new playState(gsm));
}
if(boundHelpBtn.contains(Gdx.graphics.getWidth() - screenX, Gdx.graphics.getHeight() - screenY)){
gsm.set(new helpingState(gsm));
}
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
答案 0 :(得分:3)
为什么不使用Preferences
,了解更多信息here。
它处理所有设备的所有保存,并且是libGDX API的一部分。
// Get an instance
Preferences prefs = Gdx.app.getPreferences("Highscores");
// Save a high score
prefs.putInteger("high1", 999);
// Make sure it is saved
prefs.flush();
// Get the high score (the 0 is default, in case no high1 is set yet)
int high1 = prefs.getInteger("high1", 0);
当API提供此方法时,您不应该在.txt
中保存高分。