簡體   English   中英

Libgdx首選項無法正確保存

[英]Libgdx preferences doesn't save properly

所以我在使用libgdx和使用首選項時遇到了這個問題。 首選項應該保存浮動“值”,它也會這樣做,但是當我從Android任務管理器中刪除我的應用程序或重新啟動我的Android系統時,浮動返回其默認值。 我已經讀過這可能是由float的靜態值引起的,但是我無法從float中刪除靜態因為我正在使用我的GestureDetector類的float。 我可以為GestureDetector類創建一個新浮點數並引用浮點數“值”以減少新浮點數減少時的值嗎?

這是我的主要代碼:

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();
}

}

這是我的GestureDetector類:

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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM