我是新來的機器人,所以爲了學習的興趣我做了一個簡單的遊戲,用戶在一個活動中發現一個圖像「scene1」然後,一旦他們按下它移動的圖像到下一個活動「scene2」,一切正常。我想要做的是保存用戶獲得的級別,以便他們可以在下次開始遊戲時繼續操作,例如單擊加載保存數據按鈕。如何存儲用戶在我的遊戲中的進度
我已經看過共享的首選項並寫入到內部存儲中的文件,但我似乎無法得到我的頭:(這將是非常有幫助的,如果你可以給我一些幫助,讓我搬進正確的方向:)
以下是我用於每個級別的代碼。
public class scene1 extends Activity implements OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 20 * 1000;
private final long interval = 1 * 1000;
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("FindIt")
.setMessage("Exit to main menu?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
ImageButton imageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime/1000));
}
@Override
public void onClick(View v) {
startB.setVisibility(View.INVISIBLE);
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
Intent intent = new Intent(scene1.this, timeUp.class);
scene1.this.startActivity(intent);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.it);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(scene1.this,
"You found It!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(scene1.this, scene2.class);
scene1.this.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}
});
}
}
}
哦,好傷心...'android.os.Process.killProcess(android.os.Process.myPid()); '不要那樣做!使用'finish()'是一個優雅的方式來結束一個'活動'不要像這樣殺死你的過程。 – Squonk