java - Android game 3 trials for user -
hello it's first time develop android color game. i'd put 3 trials in each question. i'm bit confused how or put while loop in code. please have on have tried far:
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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 reached 3rd trial") .setpositivebutton("try again?", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent = new intent(stage1_2.this, stage1_1.class); startactivity(i); } }) .setnegativebutton("back menu", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent = new intent(stage1_2.this, shapingcolors.class); startactivity(i); } }) .show(); } } // end of while loop
i finish thing can continue game. appreciated. in advance.
i added new method called guessedwrong()
private void guessedwrong(){ trial++; if(trial == 3){ new alertdialog.builder(this) .settitle("game over") .setmessage("sorry reached 3rd trial") .setpositivebutton("try again?", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent = new intent(stage1_1.this, stage1_1.class); startactivity(i); } }) .setnegativebutton("back menu", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent = new intent(stage1_1.this, shapingcolors.class); startactivity(i); } }) .show(); } }
the reason you're confused can't use loop task. when using java android framework, each of callback functions (for example, onclick
listener, or activity
's onresume
) has run , finish within 1 frame of application. after function returns app update screen. means if you're doing responding series of clicks, can't in loop. have handle each click in separate call callback. for
, while
loops appropriate if want iterate on list of items decide should happen right now (for example, if you're adding items listview
), can't iterate on things happen @ different times (such user's guesses).
you have think activity
state machine. make trial
member variable (field) of activity
, starts out @ 0
. might have function guessedwrong()
increments trial
, , goes "game over" screen if it's greater 2. onclick
listener wrong answers call function. when moving new question, reset trial
0
.
you need make sure number of trials (which state you're in) preserved if activity
restarted. lesson recreating activity in android developers' training offered google shows how this.