I’m new to Android and I’m not sure how to tackle this one.
I’m making a Pong like game, and after a score is made, I want to display the score for a few seconds while pausing the game.
I’m using the onDraw() method like this:
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(_bitmapStore.get(R.drawable.batblue),bluebat.getX() - (_bitmapStore.get(R.drawable.batblue).getWidth() /2),bluebat.getY(),null);
canvas.drawBitmap(_bitmapStore.get(R.drawable.ball),ball.getX() - (_bitmapStore.get(R.drawable.ball).getWidth() /2),ball.getY()-(_bitmapStore.get(R.drawable.ball).getHeight() /2), null);
canvas.drawBitmap(_bitmapStore.get(R.drawable.batred),redbat.getX() - (_bitmapStore.get(R.drawable.batred).getWidth() /2),redbat.getY(),null);
if(drawScore){
canvas.drawBitmap(_bitmapStore.get(R.drawable.user),200,200, null);
canvas.drawBitmap(_bitmapStore.get(R.drawable.cpu),400,200, null);
}
}
I want it to draw the score for a few seconds, but don’t know how to pause.
I tried the thread.sleep() method, even though I know it’s not recommended. But it didn’t do nothing except for slowing the game down.
The images are displayed only once a score is made, but the game keeps on running behind it.
I would separate out your drawing from your engine. If your engine worked something like this
you would get this for free essentially. Obviously this is abstract: you may or may not have a Drawable interface. But this will give you a lot of power over your game. Instead of having a variable to draw a score or not, you’d have an object in a list (probably a map, actually) that would get added and removed after a particular amount of time.