
Untitled
By: a guest on
May 8th, 2012 | syntax:
None | size: 1.14 KB | hits: 69 | expires: Never
Having a countdown timer on a Canvas using Surfaceview class in Android
public class GameView extends SurfaceView {
double timeleft =60,000 //60 sec
int clicked=0;
static double starttime;//time when we started game
double timeleft=300000;
@Override
protected void onDraw(Canvas canvas) {
if(clicked==0)
score.onDraw(canvas,timeleft); // trying to print the score by calling onDraw function in score class
else {
double timegone= System.currentTimeMillis() - starttime ;
timeleft=timeleft-timegone;
score.onDraw(canvas,timeleft);
}
public boolean onTouchEvent(MotionEvent event)
{
if(clicked==0)
starttime= System.currentTimeMillis();
clicked++;
}
}
}
public void onDraw(Canvas canvas, double time) {
Paint paint = new Paint();
int min=(int) (time/60000);
int sec=(int) ((time)%60000);
paint.setColor(Color.GREEN);
paint.setTextSize(20);
canvas.drawText("Coins:"+Integer.toString(coins),gameView.getWidth()-100, 20, paint);
canvas.drawText("Time Left:"+Integer.toString(min)+":"+Integer.toString(sec),gameView.getWidth()-140, 40, paint);
}