如何在游戏中显示分数?

时间:2014-01-31 01:59:17

标签: android levels

我正在制作一个类似徽标测验的游戏,但是在他正确回答之后我无法让玩家获得分数。

以下是我所知道的事情:

public class Capitolio extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guess1);
        init();

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true); 
    }

    public boolean onOptionsItemSelected(MenuItem item){
        Intent myIntent = new Intent(getApplicationContext(), Level1.class);
        startActivityForResult(myIntent, 0);
        return true;

    }


    private Button buttonSaveMem1;
    private EditText escrive; 
    private TextView respuest, otra;
    private String [] answers;
    int Score = 0;

    private int currentQuestion;



    public void init() {

        answers = new String[]{"nnnnn"};      
        buttonSaveMem1 = (Button)findViewById(R.id.button1);     
        respuest = (TextView) findViewById(R.id.textView2); 
        otra = (TextView) findViewById(R.id.textView1);
        escrive = (EditText) findViewById(R.id.editText1);
        buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);

        LoadPreferences();
    }

    Button.OnClickListener buttonSaveMem1OnClickListener = new Button.OnClickListener() {

        @Override    
        public void onClick(View arg0) {    
            checkAnswer();  
            // TODO Auto-generated method stub
            SavePreferences("MEM1", respuest.getText().toString());
            LoadPreferences();
        }
    };

    public boolean isCorrect(String answer) {     
        return (answer.equalsIgnoreCase(answers[currentQuestion]));
    } 

    /* this method :     
    * 1: Read the text ( answer) from the answerTextEdit  
    * 2: call the isCorrect method     
    * 3: display the appropriate message.    
    */ 

    public void checkAnswer()  {     
        String answer = escrive.getText().toString();  
        if(isCorrect(answer))      
            respuest.setText("You're right!" + "The Answer is The Vatican");  
        else     
            respuest.setText("Sorry, The answer is not right!");   
    }

    public void showscore() {     
        String answer = escrive.getText().toString();  
        if(isCorrect(answer))      
            Score += 1; 

        otra.setText(Score);   
    }

    private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    private void LoadPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String strSavedMem1 = sharedPreferences.getString("MEM1", "");

        respuest.setText(strSavedMem1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

在此之后我想开发一种用这个分数解锁其他关卡的方法,但我不知道如何做到这一点。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

public void checkAnswer()  {     
    String answer = escrive.getText().toString();  
    if(isCorrect(answer)) {
        respuest.setText("You're right!" + "The Answer is The Vatican");
        Score += 1;
    }
    else {
        respuest.setText("Sorry, The answer is not right!");
    }

    showScore();
}

public void showScore() {     
    otra.setText(Score);   
}