簡體   English   中英

如何檢查啟動是否是首次啟動並包括教程-LibGDX

[英]How to check whether the launch is first launch and include tutorial-LibGDX

我想為游戲的首次發布創建一個游戲教程。 下一次教程不應發生。 為此,我創建了一個布爾值以優先存儲,以指示啟動是否為第一次。

我的單例首選項類包括在這里:

public class FirstLaunchNotifier {

    public static final FirstLaunchNotifier INSTANCE = new FirstLaunchNotifier();

    private final Preferences prefs;

    private static final String LAUNCH_KEY = "launch";
    private static final String PREF_NAME = "TMS";

    /* if false->first launch with tutorial.else if true->no tutorial */
    private boolean launchBoolean;

    public FirstLaunchNotifier() {
        prefs = Gdx.app.getPreferences(PREF_NAME);
        launchBoolean = prefs.getBoolean(LAUNCH_KEY, false);
    }

    public void saveLaunchState() {
        launchBoolean = true;
        prefs.putBoolean(LAUNCH_KEY, launchBoolean);
        prefs.flush();
    }

    public boolean getLaunchBoolean() {
    //  System.out.println("launch::: "+launchBoolean);
        return launchBoolean;

    }

在我的游戲屏幕類中,當前我在render()內部調用了update()方法。

對於首次啟動的第一個游戲的教程部分,我想調用一個單獨的更新方法tutorialUpdate() 我對如何通過此update()tutorialUpdate()正確獲取這個launchBool值感到困惑。

教程完成后,游戲應返回正常播放saveLaunchState()在調用saveLaunchState()launchboolean變為true表示第一次播放結束了,不再需要教程。

這就是我現在在render()中調用update()的方式。

case RUN:
    update(delta);
break;

我需要這樣的布爾值嗎?

if(FirstLaunchNotifier.INSTANCE.getLaunchBoolean()) {
     update(delta);
     tutorial update();
}else{
     update(delta);
}

哪個是正確的方法?

FirstLaunchNotifier為單例。

public class FirstLaunchNotifier {

    private static FirstLaunchNotifier instance;
    private  Preferences prefs;
    private boolean launchBoolean;

    private static final String LAUNCH_KEY = "launch";
    private static final String PREF_NAME = "TMS";

    private FirstLaunchNotifier(){

        prefs= Gdx.app.getPreferences(PREF_NAME);
        launchBoolean = prefs.getBoolean(LAUNCH_KEY, true);
    }

    public static FirstLaunchNotifier getInstance(){

        if(instance==null){
            instance=new FirstLaunchNotifier();
        }
        return instance;
    }

    public void saveLaunchState(boolean state) {
        launchBoolean=state;
        prefs.putBoolean(LAUNCH_KEY, launchBoolean);
        prefs.flush();
    }

    public boolean getLaunchBoolean() {
        return launchBoolean;
    }
}

您可以通過FirstLaunchNotifier.getInstance()獲取FirstLaunchNotifier的對象,並根據您的要求進行計算,例如

if(FirstLaunchNotifier.getInstance().getLaunchBoolean()) {
     update(delta);
     tutorial update();
}else{
     update(delta);
}

當用戶完成教程部分后,只需致電

FirstLaunchNotifier.getInstance().saveLaunchState(false);

暫無
暫無

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

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