0

你好,这是我第一次开发安卓彩色游戏。但是,我想在每个问题中进行 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();
    }


}
4

1 回答 1

3

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

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

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

于 2013-04-25T16:11:11.963 回答