android - Wait for the countdown timer to finish or the user to click a button -
i'm trying create mini-game loop in user has 10 seconds click button or loses game. when run while game loop, want while loop start timer , either wait timer run out or user click button. also, when code runs, app crashes within while loop. not sure how continue.
i'm rather new android. in advance.
public class maingame extends activity implements onclicklistener { progressbar progress1; boolean gameon = true; button option1; button option2; button option3; private countdowntimer countdowntimer; private final long starttime = 10*1000; private final long interval= 100; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_game); log.d("mark", "justcreated completed"); initializebuttons();//also initializes timer, buttons, text, etc. log.d("mark", "initialize buttons function completed"); maingameloop(); //drawable draw = getresources().getdrawable(r.drawable.custom_progress_bar); //progress1.setprogressdrawable(draw); } private void initializebuttons(){ option1 = (button) findviewbyid(r.id.buttonchoice1); option1.setonclicklistener(this); option2 = (button) findviewbyid(r.id.buttonchoice2); option2.setonclicklistener(this); option3 = (button) findviewbyid(r.id.buttonchoice3); option3.setonclicklistener(this); countdowntimer = new mycountdowntimer (starttime, interval); log.d("mark", "inside initialize buttons function"); } private void maingameloop(){ while (gameon){ log.d("mark", "while loop running"); countdowntimer.start(); } } public class mycountdowntimer extends countdowntimer { public mycountdowntimer(long starttime, long interval){ super (starttime, interval); synchronized(this){ } } @override public void onfinish() { option1.setbackgroundcolor(getresources().getcolor(r.color.lightgreen)); } @override public void ontick(long millisuntilfinished) { } } @override public void onclick(view v) { switch(v.getid()){ case r.id.buttonchoice1: break; case r.id.buttonchoice2: break; case r.id.buttonchoice3: default: break; } } }
with this
while (gameon){ log.d("mark", "while loop running"); countdowntimer.start(); }
you run code
countdowntimer.start();
with every iteration i'm sure is not want. don't need (want?) while loop
@ all. start timer whenever need in onfinish()
run code want run when time runs out , place code in button
click well. put in 1 function , call function onfinish()
, onclick()
or whatever.
i suggest checking in onclick()
, or method runs code, cancel timer , initialize null
.
Comments
Post a Comment