所以我在使用libgdx和首选项时遇到了这个问题。首选项应该保存float"值",它也保存了,但当我从Android任务管理器中杀死我的应用程序或重新启动我的Android系统时,float会回到默认值。我读到这可能是由浮点的静态值引起的,但我无法从浮点中删除静态值,因为我正在将浮点与GestureDetector类一起使用。我可以为GestureDetector类制作一个新的浮点值,并在新浮点值减少时引用浮点值"value"来减少吗?
这是我的主要代码:
public class WorldScreen implements Screen{
Preferences prefs;
private Texture bgCity;
private Texture bgLoop;
private Texture bgEnd;
private Texture bgFade;
private Texture hud;
public static OrthographicCamera camera;
SpriteBatch batch;
Rectangle player;
Rectangle background;
Rectangle backgroundloop;
public float time = 100;
public float camx = 0;
public float camy = 0;
public static float score = 384400000f;
public static float value = 384400000f;
BitmapFont font;
GestureListenerC controller;
private Music startmusic;
private static Music loopmusic;
private Music endMusic;
public static boolean ending = false;
private int endint;
private String endstring = "";
public WorldScreen(final JetpackGame aa) {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
value += GestureListenerC.implementvalue;
camx = camera.viewportWidth / 2f;
camy = camera.viewportHeight / 2f;
controller.update();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(bgCity, background.x, background.y);
batch.draw(bgLoop, backgroundloop.x, backgroundloop.y);
batch.draw(bgEnd, 0, 5360);
if(!ending){
if(camera.position.y >= 5360) camera.position.y = 4096;
}
if(value <= 0) ending = true;
//ENDING
if(ending){
time += 0.1f * Gdx.graphics.getDeltaTime();
startmusic.stop();
loopmusic.stop();
if(time <= 102.75f){
camera.position.y += 0.15f * time;
if(camera.position.y >= 5360) camera.position.y = 4096;
}
if(time >= 102.75f) {
if (camera.position.y >= 4090 && camera.position.y <= 6666)
camera.position.y += 0.15f * time;
}
endMusic.play();
font.setScale(2.0f);
font.draw(batch, "You already finished the game xD", 0,1200);
if(time >= 103.2f)endMusic.stop();
}
Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(normalProjection);
batch.draw(hud, 0, 85, Gdx.graphics.getWidth(), 1200);
font.setScale(3.5f);
if(time <=102.5f) {
font.draw(batch, ">Meters to the moon", 10, 1260);
font.draw(batch, ">" + (int)value + "m", 10, 1190);
}
//font.draw(batch,""+time, 100, 100);
//font.draw(batch,""+value, 100,120);
if(time >= 103) {
font.setScale(3.0f);
font.draw(batch, ">"+endstring, 10,1200);
}
if(time >= 104)
batch.draw(bgFade, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.end();
if(value < 0) value = 0;
if(!startmusic.isPlaying()) {
loopmusic.setLooping(true);
loopmusic.play();
}
//RANDOM ENDING GENERATOR
switch(endint){
case 1: endstring = "Now do it again faster!";
break;
case 2: endstring = "What are you doing with your life?!";
break;
case 3: endstring = "Do you want a cookie?";
break;
case 4: endstring = "How many hours did you waste?";
break;
case 5: endstring = "So you have no life?";
break;
case 6: endstring = "Now get out of your room and go outside!";
break;
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
Random rand = new Random();
endint = rand.nextInt(6);
font = new BitmapFont();
font.setColor(Color.GREEN);
Preferences prefs = Gdx.app.getPreferences("My Preferences");
float value = prefs.getFloat("Value", 384400000f);
controller = new GestureListenerC();
GestureDetector gestureDetector = new GestureDetector(20, 0.5f, 2, 0.15f, controller);
Gdx.input.setInputProcessor(gestureDetector);
loopmusic = Gdx.audio.newMusic(Gdx.files.internal("audio/AmbientLoop.ogg"));
startmusic = Gdx.audio.newMusic(Gdx.files.internal("audio/AmbientStart.ogg"));
endMusic = Gdx.audio.newMusic(Gdx.files.internal("audio/Moon.ogg"));
endMusic.setLooping(false);
startmusic.play();
bgCity = new Texture(Gdx.files.internal("img/city_BG.png"));
bgLoop = new Texture(Gdx.files.internal("img/loopBG.png"));
bgEnd = new Texture(Gdx.files.internal("img/endBG.png"));
bgFade = new Texture(Gdx.files.internal("img/fade.png"));
hud = new Texture(Gdx.files.internal("img/Hud.png"));
camera = new OrthographicCamera();
camera.setToOrtho(false, 480,854);
batch = new SpriteBatch();
background = new Rectangle();
background.x = 0;
background.y = 0;
background.width = 479;
background.height = 4096;
backgroundloop = new Rectangle();
backgroundloop.x = 0;
backgroundloop.y = 4096;
backgroundloop.width = 512;
backgroundloop.height = 1024;
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
prefs.putFloat("Value", value);
prefs.flush();
batch.dispose();
font.dispose();
bgCity.dispose();
bgLoop.dispose();
bgEnd.dispose();
bgFade.dispose();
hud.dispose();
loopmusic.dispose();
startmusic.dispose();
endMusic.dispose();
}
}
这是我的手势检测器类:
public class GestureListenerC implements GestureListener{
public static float velX, velY;
public static boolean flinging = false;
float initialScale = 1;
public static float implementvalue = 0;
public boolean touchDown (float x, float y, int pointer, int button) {
flinging = false;
initialScale = WorldScreen.camera.zoom;
return false;
}
@Override
public boolean tap (float x, float y, int count, int button) {
//Gdx.app.log("GestureDetectorTest", "tap at " + x + ", " + y + ", count: " + count);
return false;
}
@Override
public boolean longPress (float x, float y) {
//Gdx.app.log("GestureDetectorTest", "long press at " + x + ", " + y);
return false;
}
@Override
public boolean fling (float velocityX, float velocityY, int button) {
//Gdx.app.log("GestureDetectorTest", "fling " + velocityX + ", " + velocityY);
flinging = true;
velY = WorldScreen.camera.zoom * velocityY * 0.5f;
//if(WorldScreen.value <= 0)WorldScreen.value = 0;
return false;
}
@Override
public boolean pan (float x, float y, float deltaX, float deltaY) {
// Gdx.app.log("GestureDetectorTest", "pan at " + x + ", " + y);
if(deltaY >= 0) {
if (!WorldScreen.ending) {
//if (WorldScreen.value != 0) {
WorldScreen.camera.position.add(0, Math.abs(deltaY * WorldScreen.camera.zoom), 0);
WorldScreen.value -= Math.abs(1 * deltaY);
//}
if (WorldScreen.camera.position.y <= 0) WorldScreen.camera.position.y = 0;
}
}
//if(WorldScreen.value <= 0)WorldScreen.value = 0;
return false;
}
@Override
public boolean panStop (float x, float y, int pointer, int button) {
//Gdx.app.log("GestureDetectorTest", "pan stop at " + x + ", " + y);
return false;
}
@Override
public boolean zoom (float originalDistance, float currentDistance) {
return false;
}
@Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
return false;
}
public void update () {
if(!WorldScreen.ending){
if (flinging) {
//if(WorldScreen.value != 0){
velY *= 0.98f;
if(velY >= 0) {
WorldScreen.value -= 100.5f * velY;
WorldScreen.camera.position.add(0, velY * Gdx.graphics.getDeltaTime(), 0);
}
if (velY < 0.01f) velY = 0;
//}
//if(WorldScreen.camera.position.y <= 480){ WorldScreen.camera.position.y = 480; WorldScreen.value = 384400000;}
}
}
//if(WorldScreen.value <= 0)WorldScreen.value = 0;
}
}
如果你是为android设计的,那么你可以切换到android的"SharedPreferences"