我有一个关于如何自动保存和加载游戏的问题。在这个例子中,就像Temple Run一样,它会自动保存游戏的所有内容(记录、获得的金钱和解锁的好东西),就像看起来一样简单。与 PlayStation 1 游戏相比,例如Abe's Exodus,它没有自动保存/加载功能,但它可以将游戏保存在游戏关卡中 Abe 离开的区域的确切位置以及您何时离开加载游戏,它会自动在此关卡中的最后一个位置开始游戏。
现在,我正在尝试通过我制作的这个简单的应用程序测试自动加载和保存游戏的这个功能,这是我期望成功的顺序如下:
- 首次打开应用,如果是新应用,可移动精灵默认从中心开始。
- 接下来,我尝试通过倾斜设备来移动精灵,并将其放置在顶部。
- 然后,当我使用BACK键而不是HOME键关闭应用程序时,通过处置关闭应用程序。
- 最后,当我重新打开应用程序时,只要我把它放在精灵上的那个坐标就会成为新的起点。
我的程序有一些错误。而不是顶部的精灵和这个位置应该保存这个坐标,一旦我重新打开精灵就会回到中心!我在代码工作区直接尝试了render()方法中的关键字Preferences以测试保存游戏的功能,但令人困惑,因为它似乎只读取值。
这是我关于上述主题的几个问题:
- 在 LibGDX 中使用SQL Lite for Java 来保存游戏是个好主意吗?
- 三星 Galaxy SIII等 Android 智能手机有 SD 卡插槽,但在Google Nexus 平板电脑中没有,主要问题是:Android 平板电脑中真的有外置驱动器吗?我可以通过编码将游戏保存在内部吗?
- 使用首选项是自动保存所有内容(即角色、记录、项目、设置、关卡等)并仅在关闭应用程序并重新打开后自动加载所有内容的唯一方法吗?
希望您能够帮助我。
这是我的代码:
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?
}
}