简体   繁体   中英

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

I want to create a tutorial of my game for the first launch of game. Next time tutorial should not occur. For this, I created a boolean value to be stored in preference to indicate whether the launch is first or not.

My singleton preference class is included here:

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;

    }

in my gamescreen class,currently I have update() method that I am calling inside render() .

For tutorial part of first play of first launch,I want to call a separate update method tutorialUpdate() . I am confused of how to get this launchBool value properly with this update() and tutorialUpdate() .

Once the tutorial is finished,game should come back to normal play.Before that saveLaunchState() should be called so that launchboolean become true to indicate that first play is over and no more tutorial required.

This is how I calls update() right now in render().

case RUN:
    update(delta);
break;

Do i need to get the boolean value like this?

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

Which is the proper way to do this?

FirstLaunchNotifier as a singleton.

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

You can get object of FirstLaunchNotifier by FirstLaunchNotifier.getInstance() and do calculation according to your requirement like

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

And when user completed tutorial part then just call

FirstLaunchNotifier.getInstance().saveLaunchState(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM