2013-07-02 37 views
0

我是新來的機器人,所以爲了學習的興趣我做了一個簡單的遊戲,用戶在一個活動中發現一個圖像「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(); 

     } 

    }); 
} 

} 
} 
+3

哦,好傷心...'android.os.Process.killProcess(android.os.Process.myPid()); '不要那樣做!使用'finish()'是一個優雅的方式來結束一個'活動'不要像這樣殺死你的過程。 – Squonk

回答

0

您必須創建某種「數據」或「數據文件」。這些數據將具有當前級別。您可以將其保存在內部(在用戶的手機內存中),也可以在某些服務器上進行保存。下面是你需要什麼:

http://developer.android.com/guide/topics/data/data-storage.html

你想爲你的遊戲看看是否有已經存儲數據的遊戲方式。確保在遊戲開始前(或選擇加載遊戲時)執行此項檢查。數據文件可能是一個非常簡單的文件,只有上次播放時間和當前級別。

//pseudo code 
//public onStart() 
    //look for saved file 
    //if saved file does not exist or is null 
     //new game 
    //else 
     //parse saved file 
     //load up proper level/stats/etc. 

這也是有用的保存的其他數據:

http://developer.android.com/training/basics/data-storage/index.html