Using a Timer Class to stop a Program (Java, using Motodev/Eclipse)
I'm just starting using Motodev to program for Android using Java. I am working on a Beginner's example - making a simple game program. The beginner's example suggests I look into inserting a start/stop timer to have the program end after 30 seconds and say "game over". Then I need to have the Menu option pop open for new game or exit. I am having a really difficult time figuring out how to use the Timer class to terminate the app. Can anyone help me out with this?
There are 3 classes I'm working with:
Game canvas (Super-class)
package com.dotsmasher;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class DotSmasherCanvas extends View implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(detectHit((int)event.getX(),(int)event.getY())){
score += 1;
invalidate();
}
return false;
}
int dotX, dotY, score;
public int getDotX() {
return dotX;
}
public void setDotX(int dotX) {
this.dotX = dotX;
}
public int getDotY() {
return dotY;
}
public void setDotY(int dotY) {
this.dotY = dotY;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
protected void moveDot(){
//create 2 random numbers
//assign the new numbers to dotX, dotY
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
int w = getWidth()-20;
int h = getHeight()-40;//to avoid covering score
float f = generator.nextFloat();
dotX = (int)(f*w)%w;
f = generator.nextFloat();
dotY = (int)(f*h)%h;
}
protected boolean detectHit(int x, int y){
if((x>=dotX&&x<=dotX+20)&&(y>=dotY&&y<=dotY+20)){
//we have a hit
return true;
}
return false;
}
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.BLACK);
Paint dotPaint = new Paint();
dotPaint.setColor(Color.RED);
canvas.drawRect(dotX,dotY,dotX + 20,dotY + 20,dotPaint);
canvas.drawText("Score: " + score,20,20,dotPaint);
}
public DotSmasherCanvas(Context context){
super(context);
moveDot();
setOnTouchListener(this);
}
}
TIMER CLASS
package com.dotsmasher;
import java.util.TimerTask;
import com.dotsmasher.DotSmasherCanvas;
public class DotSmasherTimerTask extends TimerTask {
DotSmasherCanvas canvas;
public DotSmasherTimerTask(DotSmasherCanvas canvas){
this.canvas = canvas;
}
@Override
public void run() {
canvas.moveDot();
canvas.postInvalidate();
}
}
MAIN CLASS
package com.dotsmasher;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.util.Timer;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setTitle("DotSmasher");
canvas = new DotSmasherCanvas(getBaseContext());
timer = new Timer();
task = new DotSmasherTimerTask(canvas);
timer.schedule(task, 0, 1500);
setContentView(canvas);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
开发者_StackOverflow社区 }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item01:
newGame();
return true;
case R.id.item02:
quit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
void newGame(){
canvas.setScore(0);
}
void quit(){
timer.cancel();
task = null;
timer = null;
canvas = null;
finish();
}
private Timer timer;
private DotSmasherCanvas canvas;
private DotSmasherTimerTask task;
}
精彩评论