针对用户的Android游戏3试用版

时间:2013-04-25 15:57:32

标签: java android

您好,这是我第一次开发Android彩色游戏。但是我想在每个问题上进行3次试验。我有点困惑在我的代码中放置while循环的方式或位置。请看看我到目前为止所尝试的内容:

  int trial = 0;

  private void getCorrectObject() {

    List<Integer> objects = new ArrayList<Integer>();
    objects.add(1);
    objects.add(2);
    objects.add(3);
    objects.add(4);
    objects.add(5);
    objects.add(6);
    objects.add(7);
    objects.add(8);
    objects.add(9);

    Collections.shuffle(objects);
    int correctObject =  objects.get(0);
    Log.d("test", String.valueOf(correctObject));



    while(trial <=3){
        trial++;


    switch(correctObject)
    {
    case 1:
        bObjectCorrect.setImageResource(R.drawable.stage1_1_object1);
        bObjectCorrect.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 2:
        bObject1.setImageResource(R.drawable.stage1_1_object1);
        bObject1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 3:
        bObject2.setImageResource(R.drawable.stage1_1_object1);
        bObject2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 4:
        bObject3.setImageResource(R.drawable.stage1_1_object1);
        bObject3.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 5:
        bObject4.setImageResource(R.drawable.stage1_1_object1);
        bObject4.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 6:
        bObject5.setImageResource(R.drawable.stage1_1_object1);
        bObject5.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 7:
        bObject6.setImageResource(R.drawable.stage1_1_object1);
        bObject6.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 8:
        bObject7.setImageResource(R.drawable.stage1_1_object1);
        bObject7.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 9:
        bObject8.setImageResource(R.drawable.stage1_1_object1);
        bObject8.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    } // Last of switch statement


    if(trial == 3){
        new AlertDialog.Builder(this)
        .setTitle("Game Over")
        .setMessage("Sorry you reached your 3rd trial")
        .setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Intent i = new Intent(Stage1_2.this, Stage1_1.class);
                startActivity(i);
            }
         })
        .setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Intent i = new Intent(Stage1_2.this, ShapingColors.class);
                startActivity(i);
            }
         })
         .show();
    }

    } // end of while loop

我真的很想完成这件事,所以我可以继续比赛。真的很感激任何帮助。提前谢谢。

我添加了一个名为 guessedWrong()

的新方法
 private void guessedWrong(){
    trial++;

    if(trial == 3){
        new AlertDialog.Builder(this)
        .setTitle("Game Over")
        .setMessage("Sorry you reached your 3rd trial")
        .setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Intent i = new Intent(Stage1_1.this, Stage1_1.class);
                startActivity(i);
            }
         })
        .setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Intent i = new Intent(Stage1_1.this, ShapingColors.class);
                startActivity(i);
            }
         })
         .show();
    }


}

1 个答案:

答案 0 :(得分:3)

您感到困惑的原因是您无法使用循环执行此任务。使用Java Android框架时,每个回调函数(例如,onClick侦听器或Activity的{​​{1}})必须在应用程序的一个帧内运行并完成。只有在函数返回后,您的应用才会更新屏幕。这意味着,如果您正在执行诸如响应一系列点击之类的操作,则无法在循环中执行此操作。您必须在单独的回调调用中处理每次单击。如果您想迭代项目列表以决定现在将会发生什么,那么onResumefor循环是合适的(例如,如果您要向while),但你不能迭代在不同时间发生的事情(例如用户的猜测)。

你必须考虑你的ListView状态机。将Activity设为trial的成员变量(字段),该变量从Activity开始。你可能有一个函数0增加guessedWrong(),如果它大于2,则进入“游戏结束”屏幕。错误答案的trial监听器将调用此函数。转到新问题时,请将onClick重置为trial

如果0重新启动,您还需要确保保留试用次数(表明您所处的状态)。 Google提供的Recreating an Activity课程Android Developers' Training向您展示了如何执行此操作。