0

Hi I'm new to android game development and I'd like to ask you guys regarding the correct logic flow for this android game of mine. So here's my game. It's a pretty simple game which user has to choose the correct color. The game has 3 stages. In each stage, it has 10 questions. In each question, it has a 30 seconds timer. with a question and choices, of course it needs to be randomized. If the user chooses the correct color, it will proceed to the next question. But if the user chooses the wrong color, he/she has only 3 trials to choose, if he/she reaches the 3rd trial, Game is OVER and display a TRY AGAIN button.

Here's a piece of code that I tried:

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

What also I'm considering is, if the user chooses the correct answer without the timer has ended, what should I put in onTick method to force the timer to end? And one of the tricky part that I face is the randomization of choices or the objects.

here is also what I've tried so far:

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

UPDATED: and another thing is, I'm confused where to put my while loop in here for 3 trials.

 // 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

I'd really love to hear your suggestions. Any help from you is truly appreciated. Thanks in advance!

asked Apr 25, 2013 at 14:09
1
  • If you're satisfied with my answer, click the check mark next to it to accept it, so other visitors know this question is answered. Commented Apr 25, 2013 at 16:42

1 Answer 1

1

You don't need to put anything in onTick to handle this case. After you've called cancel() (which you do in your click handler), onTick() won't be called again.

answered Apr 25, 2013 at 14:15
Sign up to request clarification or add additional context in comments.

4 Comments

so I have to put counter.cancel in every objects?
You have to call it from each codepath where you want the timer to stop.
Okay thank you. so my another question is which is I'm a bit confused. where do I have to put my while loop for 3 trials? Please check my updated post above FYI.
Please don't ask more than one question in a Stack Overflow question. It really doesn't work well with the site's Q&A format. If you have another question, click the Ask Question button to make a new post.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.