0

嗨,我是 android 游戏开发的新手,我想问你们关于我的这个 android 游戏的正确逻辑流程。所以这是我的游戏。这是一个非常简单的游戏,用户必须选择正确的颜色。游戏有3个阶段。在每个阶段,它有 10 个问题。在每个问题中,它都有一个 30 秒的计时器。有了问题和选择,当然需要随机化。如果用户选择了正确的颜色,它将进入下一个问题。但是如果用户选择了错误的颜色,他/她只有 3 次可供选择,如果他/她达到第 3 次,则游戏结束并显示再次尝试按钮。

这是我尝试过的一段代码:

    // I created a custom countdown timer c/o Say
    counter = new MyCount(30000,1000);
     counter.start();
     // Call for correct object
    getCorrectObject();


    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.pause:
        if(mLastResourceId == R.drawable.pause){
            pause.setImageResource(R.drawable.resume);
            mLastResourceId = R.drawable.resume;
            counter.cancel();
        } else if (mLastResourceId == R.drawable.resume) {
            pause.setImageResource(R.drawable.pause);
            mLastResourceId = R.drawable.pause;
            counter = new MyCount(s1,1000);
            counter.start();
        }
        break;
  }

 public class MyCount extends CountDownTimer
 {
    public MyCount(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }
    @Override     
    public void onFinish()
    {
        Intent i = new Intent(getApplicationContext(), Stage1_2.class);
        startActivity(i);
    }

    TextView tx = (TextView) findViewById(R.id.timer);
    @Override     
    public void onTick(long millisUntilFinished)
    {
        s1 = millisUntilFinished;
        tx.setText("" + millisUntilFinished / 1000);
    }
}

我还在考虑的是,如果用户在计时器结束的情况下选择了正确答案,我应该在 onTick 方法中输入什么来强制计时器结束?我面临的棘手部分之一是选择或对象的随机化。

到目前为止,这也是我尝试过的:

 private void getCorrectObject() {
    // TODO Auto-generated method stub

    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);

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

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

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

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

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

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

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

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

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    }
}

更新:另一件事是,我很困惑将我的 while 循环放在这里进行 3 次试验。

 // I will put 3 trials logic here
    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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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) {
                // TODO Auto-generated method stub
                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();
    }

    } // Last of while loop

我真的很想听听你的建议。非常感谢您的任何帮助。提前致谢!

4

1 回答 1

1

你不需要投入任何东西onTick来处理这种情况。在您调用后cancel()(您在点击处理程序中执行此操作),onTick()将不会再次被调用。

于 2013-04-25T14:15:54.250 回答