簡體   English   中英

如何做到這一點,當我按下加載游戲時,它不僅會加載信息,還會讓你進入 state 的游戲?

[英]How to make it so when I press load game it not only loads the information but brings you into the game in that state?

我正在制作游戲,並希望加載按鈕將玩家帶回使用共享偏好保存的游戲中。 加載方法是否可能缺少某些東西? 加載方法和按鈕在主菜單中的不同活動中,但您可以使用菜單按鈕返回 go

public static final String MY_PREFS = "prefs";
public static final String POSITION = "position";
public static final String INVENTORY = "inventory";
SharedPreferences sharedPrefs;

SaveButton = findViewById(R.id.SaveButton);
SaveButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        saveData();
    }
});

MenuButton = findViewById(R.id.MenuButton);
MenuButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        finish();
    }
});

} //  public void setupControls()

public void saveData() {

    sharedPrefs = getSharedPreferences(MY_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor edit = sharedPrefs.edit();
    edit.putInt(POSITION, thePlayer.getPlayerPos());
    edit.putString(INVENTORY, thePlayer.getInventory());
    edit.commit();

    Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
}

//The code below here is in a different activity

loadGame_button = findViewById(R.id.loadGame_button);
loadGame_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        loadData();
    }
});

public void loadData() {
    SharedPreferences sharedPreferences = getSharedPreferences(MY_PREFS, MODE_PRIVATE);
    inv = sharedPreferences.getString(INVENTORY, "");
    pos = sharedPreferences.getInt(POSITION, 0);
}

試試這個:在 draw() 中將你的游戲分成不同的狀態: mainmenugame pausemenugameoverscreen 在任何不是game的 state 中設置加載按鈕,並使其除了加載您的信息外,還將 state 切換到game

暫無
暫無

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

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