android - While loops in ondraw -
in on draw made game. zombie comes , have click on him go start , not make house. wanted add pause game though wrap in while loop... when tried when app opens opens black screen. here of code.
@override protected void ondraw(canvas canvas) { super.ondraw(canvas); try { thread.sleep(speed); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } text.settextsize(50); white.setcolor(color.white); nightgreen.setcolor(color.parsecolor("#003300")); daygreen.setcolor(color.parsecolor("#339900")); clear.setargb(0, 0, 0, 0); grass.set(0, canvas.getheight()/2, canvas.getwidth(), canvas.getheight()); androiddudehitbox.set((int) androidx, (int) androidy, (int) androidx + androiddude.getwidth(),(int) androidy + androiddude.getheight()); doorhitbox.set(canvas.getwidth()/2 + 360, canvas.getheight()/2 - house.getheight()/2, canvas.getwidth()/2 + 300 + house.getwidth() - 10, canvas.getheight()/2 - house.getheight() + house.getheight()); zombiehitbox.set((int) zombiex, (int) zombiey, (int) zombiex + zombie.getwidth(), (int) zombiey + zombie.getheight()); playpausehitbox.set(0, canvas.getheight() - pause.getheight(), pause.getwidth(), 25); while (pauseplay) { switch ((int) possition) { case 0: canvas.drawcolor(color.parsecolor("#66ffff")); canvas.drawbitmap(sun, (int) sunx, (int) suny, null); canvas.drawrect(grass, daygreen); canvas.drawbitmap(house, canvas.getwidth()/2 + 300, canvas.getheight()/2 - house.getheight(), null); canvas.drawbitmap(androiddude, (int) androidx - (androiddude.getwidth()/2), (int) androidy - (androiddude.getheight()/2), null); text.setcolor(color.black); if (counter < 300) canvas.drawtext("quick! in hous before nightfall!", 160, canvas.getheight()/2 + 100, text); text.setcolor(color.red); //log.d("counter", "c: counted " + counter); if (counter < 110) {suny --; counter++;} if (counter >= 110 && counter < 240) {suny --; sunx ++; counter++;} if (counter >= 240 && counter < 360) {suny -= 0.5; sunx ++; counter++;} if (counter >= 360 && counter < 662) {suny -= 0.333; sunx ++; counter++;} if (counter >= 662 && counter < 1004) {suny += 0.333; sunx++; counter++;} if (counter >= 1004 && counter < 1104) {suny += 0.5; sunx++; counter++;} if (counter >= 1104 && counter < 1224) {suny ++; sunx++; counter++;} if (counter >= 1224 && counter < 1345) {suny ++; counter++;} if (counter >= 1345) { canvas.drawcolor(color.parsecolor("#00001a")); canvas.drawrect(grass, nightgreen); canvas.drawbitmap(house, canvas.getwidth()/2 + 300, canvas.getheight()/2 - house.getheight(), null); canvas.drawbitmap(moon, canvas.getwidth() - 300, 10, null); canvas.drawtext("you didn't make before night fall , killed", 25, canvas.getheight()/2 + 100, text); canvas.drawtext("by fears of night!", canvas.getwidth()/2 - 200, canvas.getheight()/2 + 150, text); } break; case 1: canvas.drawcolor(color.parsecolor("#00001a")); canvas.drawbitmap(moon, (int)moonx, (int)moony, null); canvas.drawrect(grass, nightgreen); canvas.drawbitmap(housedude, canvas.getwidth()/2 + 300, canvas.getheight()/2 - house.getheight(), null); canvas.drawbitmap(pause, 0, canvas.getheight() - pause.getheight() - 25, null); canvas.drawbitmap(zombie, (int) zombiex, (int) zombiey, null); if (zombiex < canvas.getwidth()/2 + 300) zombiex += 10; else { canvas.drawcolor(color.parsecolor("#00001a")); canvas.drawrect(grass, nightgreen); canvas.drawtext("the monsters got you!", 150, canvas.getheight()/2 + 100, text); canvas.drawbitmap(house, canvas.getwidth()/2 + 300, canvas.getheight()/2 - house.getheight(), null); canvas.drawbitmap(moon, canvas.getwidth() - 300, 10, null); zombiex = canvas.getwidth() + 100; canvas.drawbitmap(play, 0, canvas.getheight() - play.getheight() - 25, null); } if (counter2 < 110) {moony --; counter2++;} if (counter2 >= 110 && counter2 < 240) {moony --; moonx ++; counter2++;} if (counter2 >= 240 && counter2 < 360) {moony -= 0.5; moonx ++; counter2++;} if (counter2 >= 360 && counter2 < 662) {moony -= 0.333; moonx ++; counter2++;} if (counter2 >= 662 && counter2 < 1004) {moony += 0.333; moonx++; counter2++;} if (counter2 >= 1004 && counter2 < 1104) {moony += 0.5; moonx++; counter2++;} if (counter2 >= 1104 && counter2 < 1224) {moony ++; moonx++; counter2++;} if (counter2 >= 1224 && counter2 < 1345) {moony ++; counter2++;} if (counter2 >= 1345) { canvas.drawtext("you killed " + integer.tostring(zombieskilled) + " monsters!", canvas.getwidth()/2 - 100, canvas.getheight()/2 + 100, text); zombiex = canvas.getwidth() + 100; sunx = canvas.getwidth()/2 - sun.getwidth()/2; suny = canvas.getheight(); canvas.drawcolor(color.parsecolor("#66ffff")); canvas.drawbitmap(sun, canvas.getwidth()/2, 0, null); canvas.drawrect(grass, daygreen); canvas.drawbitmap(housedude, canvas.getwidth()/2 + 300, canvas.getheight()/2 - house.getheight(), null); canvas.drawbitmap(play, 0, canvas.getheight() - play.getheight() - 25, null); } break; } canvas.drawtext("score " + integer.tostring(score), 0, 10, white); canvas.drawrect(androiddudehitbox, clear); canvas.drawrect(doorhitbox, clear); if (androidx > canvas.getwidth() || androidx < 0 || androidy > canvas.getheight() || androidy < 0) {androidx = 30; androidy = canvas.getheight()/2;} if (doorhitbox.contains((int) androidx, (int) androidy)) possition = 1; } invalidate(); }
no, no, no. don't ever delay inside ondraw. don't put game logic in ondraw. draw. reason have black screen ondraw not returning give system chance put drawing onto physical screen.
instead, create separate thread controls game logic (including timing). can post events or runnables handler update game display.
see article painless threading introduction important issue.
Comments
Post a Comment