0

我正在创建一个包含 3 个问题/级别的测验。我的问题是我不知道要实现什么或编写如何计算正确答案的代码。例如,用户单击正确的答案(按钮)它将计数并添加,直到他达到第 3 级。在他完成游戏后,我的 complete.class 将弹出并有一个按钮,如果用户单击 highscore(button),它将自动将文本设置为我的 highscore.class

levelone.class

public class EasyOne extends Activity {

ImageButton a, b, c;
Intent intent ;
CountDownTimer cdt;
TextView timer;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.easyone);
    a = (ImageButton) findViewById(R.id.ib_a);
    b = (ImageButton) findViewById(R.id.ib_b);
    c = (ImageButton) findViewById(R.id.ib_c);
    timer = (TextView) findViewById(R.id.tv_timer);
    cdt = new CountDownTimer(5000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {
               timer.setText("seconds remaining: " + millisUntilFinished / 1000);

        }

        @Override
        public void onFinish() {
              timer.setText("TIMES UP!");
            intent = new Intent(getApplicationContext(),TimesUp.class);
            startActivity(intent);

        }
    };

    intent = new Intent(getApplicationContext(),ChoiceTwo.class);
    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(intent);


            cdt.cancel();
            Intent intent = new Intent(getApplicationContext(),ChoiceTwo.class);
            startActivity(intent);


        }
    });


    cdt.start();
}
}

complete.class
公共类完成扩展 Activity {

Button highscore;
String highestScore;
int score;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
    // TODO Auto-generated method stub
highscore = (Button) findViewById(R.id.save);
highscore.setOnClickListener(new View.OnClickListener() {

    @Override   
       public void onClick(View v) {

       SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
       if (score > preferences.getInt("score", 0)) {
            preferences.edit().putInt("score", score).commit();
       }
       Intent showHighScore = new Intent(MainActivity.this, highscore.class);
       showHighScore.putExtra("current_score", score);
       startActivity(showHighScore);
    }
});
}
}

高分.class

public class highscore extends Activity {
Button back;
TextView highScore;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highscore);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    int currentScore = getIntent().getIntExtra("current_score", 0);
    highScore = (TextView) findViewById(R.id.tv_highscore);
    highScore.setText(currentScore + " / " + preferences.getInt("score", 0));
    int highestScore = -1;
//Now use this score variable to set anywhere.
    Bundle extras = getIntent().getExtras();

    if (extras != null) {
         highestScore = extras.getInt("score");
        }


    back = (Button) findViewById(R.id.btn_back);
    back.setOnClickListener(new View.OnClickListener() {
        @Override   
       public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Back",
                    Toast.LENGTH_SHORT).show();

    }
 });    
    // TODO Auto-generated method stub
}
}
4

4 回答 4

0

您可以像这样创建一个跟踪分数的类:

    public class HudScore {

        private int score;

        public HudScore(){
            score = 0;
        }

        public void reset(){
            score = 0;
        }

        public int getScore() {
            return score;
        }

        public void addPoints() {
            this.score +=50 ;
        }

    }

然后在您的活动中创建此类的一个实例:

public static final HudScore score = new HudScore();

您还可以编写一个 textView 以在布局中显示分数:

Tectview scoreView;

对于您可以致电的每个正确答案:

score.addPoints();

并编写一个 refreshscore() 函数来更新 scoreview textView:

    public void refreshScore(){
        runOnUiThread(new Runnable() {  
        @Override
        public void run() {
            YourActivity.scoreView.setText("SCORE: "+score.getScore());
        }
        });
    }

现在为 Highscore 创建另一个类,如下所示:

    public class Highscore {

        private SharedPreferences preferences;
        private String names[];
        private long score[];

        public Highscore(Context context) {
            preferences = context.getSharedPreferences("Highscore", 0);
            names = new String[10];
            score = new long[10];
            for (int x = 0; x < 10; x++) {
                names[x] = preferences.getString("name" + x, "-");
                score[x] = preferences.getLong("score" + x, 0);
            }
        }

        public String getName(int x) {
            // get the name of the x-th position in the Highscore-List
            return names[x];
        }

        public long getScore(int x) {
            // get the score of the x-th position in the Highscore-List
            return score[x];
        }

        public boolean inHighscore(long score) {
            // test, if the score is in the Highscore-List
            int position;
            for (position = 0; position < 10 && this.score[position] >= score; position++)
                ;
            if (position == 10)
                return false;
            return true;
        }

        public boolean addScore(String name, long score) {
            // add the score with the name to the Highscore-List
            int position;
            for (position = 0; position < 10 && this.score[position] > score; position++);

            if (position == 10)
                return false;
            for (int x = 9; x > position; x--) {
                names[x] = names[x - 1];
                this.score[x] = this.score[x - 1];
            }

            this.names[position] = new String(name);
            this.score[position] = score;
            SharedPreferences.Editor editor = preferences.edit();
            for (int x = 0; x < 10; x++) {
                editor.putString("name" + x, this.names[x]);
                editor.putLong("score" + x, this.score[x]);
            }
            editor.commit();
            return true;
        }
    }

现在在您的主要活动中创建一个 HighScore 类的实例,并在用户达到高分时触发警报对话框,如下所示:

final Highscore highscore = new Highscore(getApplicationContext());

if(highscore.inHighscore(score.getScore()))
{
runOnUiThread(new Runnable() {

    @Override
    public void run() {
        AlertDialog.Builder alert = new AlertDialog.Builder(GameActivity.this);

        alert.setTitle("HIGHSCORE");
        alert.setIcon(R.drawable.trophy);
        alert.setMessage("Score : "+score.getScore()+"\nEnter your name:");

        final LinearLayout layout = new LinearLayout(GameActivity.this);
        final EditText input = new EditText(GameActivity.this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        input.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        layout.setPadding(20, 0, 20, 0);
        layout.addView(input);

        alert.setView(layout);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value = input.getText().toString();

            highscore.addScore(value,score.getScore());
          finish();
          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
              finish();
          }
        });

        alert.show();
    }
});


Hope this answers your question.
于 2013-08-07T07:16:19.277 回答
0

通过仅使用静态变量,一旦应用程序退出,它将不会保留高分值。您还需要将该值保存到 SharePreference。

于 2013-08-07T07:20:19.277 回答
0

为您的 high_score 创建一个静态变量,public static int HIGH_SCORE并在每次用户正确回答时增加它。游戏结束时显示。

于 2013-08-07T07:07:00.067 回答
0

您可以使用以下代码:

公共类 HudScore {

    private int score;

    public HudScore(){
        score = 0;
    }

    public int getScore() {
        return score;
    }

    public void addPoints() {
        this.score +=50 ;
    }

}

在 MainActivity 中:

public static final HudScore score = new HudScore(); score.addPoints();

最后只需与您设置的高分进行比较。如果它在上面,请将其保存在 sharedPrefernce..

于 2013-08-07T08:00:01.323 回答