Android游戏上的自动保存和自动加载游戏概念,以及内部与外部(A.K.A.SD卡)上的保存



我有一个关于如何自动保存和加载游戏的问题。在这个例子中,就像Temple Run一样,它会自动保存游戏中的所有内容(记录、获得的钱和解锁的好东西),就像看起来一样简单。与PlayStation 1游戏相比,比方说Abe's Exodus,它没有自动保存/加载功能,但它可以在Abe离开的游戏级别内的确切位置保存游戏,当你加载游戏时,它会在该级别内剩下的最后一个位置自动启动游戏。

现在,我正试图通过我制作的这个简单的应用程序来测试自动加载和保存游戏的这一功能,以下是我期望成功的序列:

  1. 第一次打开应用程序,如果该应用程序是新的,则默认情况下可移动的精灵将从中心开始
  2. 接下来,我试图通过倾斜设备来移动精灵,并没有将其放置在顶部
  3. 然后,当我使用BACK键而不是HOME
  4. 最后,当我重新打开应用程序时,无论何时我将其放置在精灵上的坐标都将是新的起点

我的程序有一些错误。精灵不是在顶部,这个位置应该保存这个坐标,而是在我重新打开后回到中心!我直接在代码工作区上尝试了render()方法中的关键字Preferences,以测试保存游戏功能,但令人困惑,因为它似乎只读取值。

以下是我对上述主题的几个问题:

  1. 在LibGDX中使用SQL Litefor Java来保存游戏是个好主意吗
  2. 三星Galaxy SIII这样的安卓智能手机有SD卡插槽,但在谷歌Nexus平板电脑中没有,主要问题是:安卓平板电脑中真的有外部驱动器吗?我可以通过编码将游戏保存在内部吗
  3. 使用首选项是否是自动保存所有内容(即字符、记录、项目、设置、级别等)并仅在关闭应用程序并重新打开后自动加载所有内容的唯一方法

希望你能帮助我。

这是我的代码:

Save_Load_Test.java

package com.test.save_and_load_test;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.GdxRuntimeException;
public class Save_and_Load_Test implements Screen 
{
private OrthographicCamera camera;
private Texture texture;
private Texture background;
private SpriteBatch batch;
private Rectangle pos;
private Rectangle BG_pos;
private float w = 720;
private float h = 1280;
Start game;
public Save_and_Load_Test(Start game)
{
this.game = game;
}
@Override
public void render(float delta) 
{
// TODO render()
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(background, BG_pos.x, BG_pos.y);
batch.draw(texture, pos.x, pos.y);
batch.end();
if(Gdx.input.getAccelerometerX() >= 1 && pos.x >= 0)
{
pos.x -= 20;
}
if(Gdx.input.getAccelerometerX() <= -1 && pos.x <= (720 - pos.width))
{
pos.x += 20;
}
if(Gdx.input.getAccelerometerY() >= 1 && pos.y >=0)
{
pos.y -= 20;
}
if(Gdx.input.getAccelerometerY() <= -1 && pos.y <= (1280 - pos.height))
{
pos.y += 20;
}
if(Gdx.input.isKeyPressed(Keys.RIGHT) && pos.x <= (720 - pos.width - 35))
{
pos.x += 20;
}
if(Gdx.input.isKeyPressed(Keys.LEFT) && pos.x >=0)
{
pos.x -= 20;
}
if(Gdx.input.isKeyPressed(Keys.UP) && pos.y <= (1280 - pos.height - 35))
{
pos.y += 20;
}
if(Gdx.input.isKeyPressed(Keys.DOWN) && pos.y >= 0)
{
pos.y -= 20;
}
Preferences prefs = Gdx.app.getPreferences("my-preferences");
prefs.putFloat("X", pos.x);
prefs.putFloat("Y", pos.y);
prefs.flush();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() 
{
// TODO show()
camera = new OrthographicCamera();
camera.setToOrtho(false, w, h);
texture = new Texture(Gdx.files.internal("Jeff_the_Happy_Clown.png"));
background = new Texture(Gdx.files.internal("babe_BG.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
background.setFilter(TextureFilter.Linear, TextureFilter.Linear);
batch = new SpriteBatch();
pos = new Rectangle();
pos.width = 100;
pos.height = 100;
pos.x = (w/2) - (pos.width/2);
pos.y = (h/2) - (pos.height/2);
BG_pos = new Rectangle();
BG_pos.width = background.getWidth();
BG_pos.height = background.getHeight();
BG_pos.x = (w/2) - (BG_pos.width/2);
BG_pos.y = (h/2) - (BG_pos.height/2);
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() 
{
// TODO dispose
batch.dispose();
texture.dispose();
}
}

启动.java

package com.test.save_and_load_test;
import com.badlogic.gdx.Game;
public class Start extends Game
{
@Override
public void create() 
{
setScreen(new Save_and_Load_Test(this));
}
@Override
public void resume()
{
// Is this method involved for loading the last file game saved?
}
}

您应该从保存的首选项文件中初始化resumeshow方法中的pos。类似于:

Preferences prefs = Gdx.app.getPreferences("my-preferences");
pos.x = prefs.getFloat("X", 99.0f); // XXX use a better default
pos.y = prefs.getFloat("Y", 99.0f); // XXX use a better default

如果计算出合理的默认值,就不必担心初始情况。

从技术上讲,您可以在render的顶部执行此操作(在通常情况下,您只需重新读取上次渲染中保存的值,但在这是第一次调用render的情况下,它应该会有所帮助)。但这似乎有点浪费(在渲染线程上做不必要的事情,尤其是IO,是个坏主意)。

在每次渲染中,flush都有点过分,并且当IO暂停渲染线程时,偶尔会导致打嗝。您应该能够在pause中仅flush首选项对象,因为您的应用程序即将推出。(当然,您必须在实例中保留对Preferences对象的引用。)

本问题末尾的其他问题应作为单独的问题发布。在你这么做之前,请务必阅读http://developer.android.com/guide/topics/data/data-storage.html

最新更新