【问题标题】:How to add Preferences in Android Studio(Gdx) game如何在 Android Studio(Gdx) 游戏中添加首选项
【发布时间】:2020-10-12 23:35:28
【问题描述】:

我已经尝试了很多,在我的游戏中添加偏好,但是在实施不同类型的解决方案后,我的问题没有解决。所以,请指导我如何在我的游戏中添加偏好并完成我的工作。并指定我应该在哪个部分使用“首选项”。 (例如:- 在 create、render 或 dispose 部分中)。

我提供的代码将不包含偏好代码,因为我不确定在哪里添加它。

代码:-

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
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.Intersector;
import com.badlogic.gdx.math.Rectangle;


import java.util.ArrayList;
import java.util.Random;

import sun.rmi.runtime.Log;

public class CoinMan extends ApplicationAdapter {
    SpriteBatch batch;
    Texture background;
    Texture[] man;
    int manState = 0;
    int pause = 0;
    float gravity = 0.2f;
    float velocity = 0;
    int manY = 0;
    Rectangle manRectangle;
    BitmapFont font;
    Texture dizzy;
    int score = 0;
    int gameState = 0;

    Random random;

    ArrayList<Integer> coinXs = new ArrayList<Integer>();
    ArrayList<Integer> coinYs = new ArrayList<Integer>();
    ArrayList<Rectangle> coinRectangles =  new ArrayList<Rectangle>();
    Texture coin;
    int coinCount;

    ArrayList<Integer> bombXs = new ArrayList<Integer>();
    ArrayList<Integer> bombYs = new ArrayList<Integer>();
    ArrayList<Rectangle> bombRectangles =  new ArrayList<Rectangle>();
    Texture bomb;
    int bombCount;


    @Override
    public void create () {
        batch = new SpriteBatch();
        background = new Texture("bg.png");
        man = new Texture[4];
        man[0] = new Texture("frame-1.png");
        man[1] = new Texture("frame-2.png");
        man[2] = new Texture("frame-3.png");
        man[3] = new Texture("frame-4.png");

        manY = Gdx.graphics.getHeight() / 2;

        coin = new Texture("coin.png");
        bomb = new Texture("bomb.png");
        random = new Random();

        dizzy = new Texture("dizzy-1.png");

        font = new BitmapFont();
        font.setColor(Color.WHITE);
        font.getData().setScale(10);
    }

    public void makeCoin() {
        float height = random.nextFloat() * Gdx.graphics.getHeight();
        coinYs.add((int)height);
        coinXs.add(Gdx.graphics.getWidth());
    }

    public void makeBomb() {
        float height = random.nextFloat() * Gdx.graphics.getHeight();
        bombYs.add((int)height);
        bombXs.add(Gdx.graphics.getWidth());
    }

    @Override
    public void render () {
        batch.begin();
        batch.draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

        if (gameState == 1) {
            // GAME IS LIVE
            // BOMB
            if (bombCount < 250) {
                bombCount++;
            } else {
                bombCount = 0;
                makeBomb();
            }

            bombRectangles.clear();
            for (int i=0;i < bombXs.size();i++) {
                batch.draw(bomb, bombXs.get(i), bombYs.get(i));
                bombXs.set(i, bombXs.get(i) - 8);
                bombRectangles.add(new Rectangle(bombXs.get(i), bombYs.get(i), bomb.getWidth(), bomb.getHeight()));
            }

            // COINS
            if (coinCount < 100) {
                coinCount++;
            } else {
                coinCount = 0;
                makeCoin();
            }

            coinRectangles.clear();
            for (int i=0;i < coinXs.size();i++) {
                batch.draw(coin, coinXs.get(i), coinYs.get(i));
                coinXs.set(i, coinXs.get(i) - 4);
                coinRectangles.add(new Rectangle(coinXs.get(i), coinYs.get(i), coin.getWidth(), coin.getHeight()));
            }

            if (Gdx.input.justTouched()) {
                velocity = -10;
            }

            if (pause < 8) {
                pause++;
            } else {
                pause = 0;
                if (manState < 3) {
                    manState++;
                } else {
                    manState = 0;
                }
            }

            velocity += gravity;
            manY -= velocity;

            if (manY <= 0) {
                manY = 0;
            }
        } else if (gameState == 0) {
            // Waitng to start
            if (Gdx.input.justTouched()) {
                gameState = 1;
            }
        } else if (gameState == 2) {
            // GAME OVER
            if (Gdx.input.justTouched()) {
                gameState = 1;
                manY = Gdx.graphics.getHeight() / 2;
                score = 0;
                velocity = 0;
                coinXs.clear();
                coinYs.clear();
                coinRectangles.clear();
                coinCount = 0;
                bombXs.clear();
                bombYs.clear();
                bombRectangles.clear();
                bombCount = 0;
            }
        }

        if (gameState == 2) {
            batch.draw(dizzy, Gdx.graphics.getWidth() / 2 - man[manState].getWidth() / 2, manY);
        } else {
            batch.draw(man[manState], Gdx.graphics.getWidth() / 2 - man[manState].getWidth() / 2, manY);
        }
        manRectangle = new Rectangle(Gdx.graphics.getWidth() / 2 - man[manState].getWidth() / 2, manY, man[manState].getWidth(), man[manState].getHeight());

        for (int i=0; i < coinRectangles.size();i++) {
            if (Intersector.overlaps(manRectangle, coinRectangles.get(i))) {
                score++;

                coinRectangles.remove(i);
                coinXs.remove(i);
                coinYs.remove(i);
                break;
            }
        }

        for (int i=0; i < bombRectangles.size();i++) {
            if (Intersector.overlaps(manRectangle, bombRectangles.get(i))) {
                Gdx.app.log("Bomb!", "Collision!");
                gameState = 2;
            }
        }

        font.draw(batch, String.valueOf(score),100,200);

        batch.end();
    }
    
    @Override
    public void dispose () {
        batch.dispose();
    }
}

【问题讨论】:

  • 别忘了处理掉您的 BitmapFont 和所有纹理。

标签: java android-studio libgdx preferences


【解决方案1】:

这是一个简单的例子。创建一个存储所有持久值并在更改它们时备份它们的类。在初始化时,您可以恢复任何保存的值或应用它们的默认值。

public class PersistedVariables {

    private final Preferences prefs = Gdx.app.getPreferences("PersistedVariables");

    // The variables. Restore the saved values or apply defaults.
    private int highScore = prefs.getInt("highScore", 0);
    private boolean musicOn = prefs.getBoolean("musicOn", true);

    public int getHighScore() {
        return highScore;
    }

    public void setHighScore(int highScore){
        this.highScore = highScore;
        prefs.putInt("highScore", highScore);
        prefs.flush();
    }

    public boolean isMusicOn() {
        return musicOn;
    }

    public void setMusicOn(boolean musicOn) {
        this.musicOn = musicOn;
        prefs.putBoolean("musicOn", musicOn);
        prefs.flush();
    }

}

create() 中实例化此类并将其存储在字段引用中。在需要使用这些值的任何地方,调用persistedVariables 的getter 和setter。

// In game class (for example):

private PersistedVariables persistedVariables = new PersistedVariables();

如果您愿意,将此类设为单例是安全的,只要您在 create 方法之前不尝试获取实例。 Preferences 实例不依赖于特定的 LibGDX 会话,但它使用对 Gdx 的引用来获取 Preferences 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2014-08-03
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多