Android - How to call an intent(activity) inside the onDraw(Canvas) properly? -


i'm trying call intent inside canvas doesn't seem work way thought should be. have in mind when passes through startactivity(), that's it, exits canvas right away wrong.

flow should be:

menu -> pong(activity) -> canvas -> game_over 

what's happening:

menu -> pong(activity) -> canvas(screen stuck here) -> game_over(iterates unknown times) 

sometimes crashes , won't proceed game_over.

inside ondraw() code below when game on condition met:

intent = new intent(getcontext(), gameover.class); i.putextra("score", integer.tostring(ball.getscore())); i.putextra("level", integer.tostring(ball.getlevel())); getcontext().startactivity(i); 

from i've observed after making scores, seems looping 20+ times since toast "high score" found in next activity won't stop , screen stuck @ canvas until loop stops. gave me clue activity called looping countless times.

who's @ fault here? ondraw() redrawing though started startactivity() already? if yes, how stop ondraw() looping before reaches intent won't loop next activity?

here's ondraw():

public void ondraw(canvas canvas) {     // background     canvas.drawcolor(color.dkgray);      // score     canvas.drawtext("score: " + integer.tostring(ball.getscore()), 25, 50,             paint);      // level     canvas.drawtext("level: " + integer.tostring(ball.getlevel()), 25, 100,             paint);      // =========================     // draw paddle     // paint.setcolor(color.parsecolor("#aa00ffff"));     paint.setcolor(color.argb(170, 0, 255, 255));     paint.setstrokewidth(20);     canvas.drawline(padx, pady, padx + padwidth, pady, paint);      // draw ball     canvas.drawcircle(ball.getx(), ball.gety(), ball_radius, paint);      if (ball.getscore() % 200 == 0) {         // counter = counter + 1;         if (ball.getlevel() < max_level) {             ball.setspeedx(ball.getspeedx() * 0.25f);             ball.setspeedy(ball.getspeedy() * 0.25f);         }     }      postinvalidate();      if (ball.gety() + (float) ball_radius >= y - 6) {         if (ball.getx() + ball_radius / 2 > padx                 && ball.getx() - ball_radius / 2 < padx + padwidth) {             ball.setgameover(false);         }          else {             ball.setgameover(true);             canvas.drawtext("game over! please wait.", x / 2 - 170, y / 3,                     paint);              intent = new intent(getcontext(), gameover.class);             i.putextra("score", integer.tostring(ball.getscore()));             i.putextra("level", integer.tostring(ball.getlevel()));             getcontext().startactivity(i);         }      } } 

ondraw can called 60 times second, should draw, not handle activity flow. minimal change add boolean , make sure call startactivity once. better solution move activity managemen out of drawing, , state management logic


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -