Reputation: 11
My problem is that , i am making one game and i am using Surfaceview class and drawing bitmap images on a canvas.
I want the game to have a maximum time limit of 1 minute ie 60,000 millisec.
I want to show the time on the canvas and once game starts it should start decrementing and once it becomes zero the game ends.
What i did was 1st i calculated the System. System.currentTimeMillis() initially when game begins and then subtract the Currenttime i calculate inside onDraw() function inside the class , on draw is called again n again by gameloopthread. i subtract timegone= currenttime - starttime .
initially time left was = 60000 i keep subracting it from timeleft timeleft=timeleft-timegone
and then try to use Paint class to write the time left on the canvas after formatting it to convert it into seconds.
but it gives incorrect timings and it changes in weird way sometimes +ve aur -ve and changes randomly.
I also tried to use Timer class but i dont know which listener i can pass on for it in Surfaceview class bcoz we cant have View components like buttons etc in surfaceview.
please help me in this regards if you can suggest some nice solution or point out where i am making a mistake.
thanks and regards, Mayank
Upvotes: 0
Views: 3133
Reputation: 11
here are some main relevant part of the code ... what i am doing is .. when user clicks onTouchEvent is called and current time stored in starttime variable. then inside ondraw func i calculate current time and using start time i calculate time left and then pass the value to onDraw function in score class where i can print the score and the time left. this all happens in a loop. i hve one variable called clicked so starttime will be set only once when we click 1st time ... but still i get weird results ..
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++;
}
}
}
then this onDraw function inside score class i used to print the time and other score
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);
}
Upvotes: 0
Reputation: 1362
You have the right logic, are you using the Calendar class when setting times? also when you used StandardDateFormat to format the time, you must specify a timezone, same as when you use the calendar? please provide code of what your doing...
Upvotes: 0