我在android中编写一个非常简单的游戏,游戏就像触摸一个形状(如矩形,圆形,弧形......),为此,我编写一个Thread MainGameTread.class:
package com.example.komeil.surfaceview_2;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import java.util.Random;
/**
* Created by Komeil on 04/11/2016.
*/
public class MainGameThread extends Thread {
private boolean running;
private SurfaceHolder surfaceHolder;
private MainGameView mainGameView;
private String shapeType;
private Random rnd;
private int xPos;
private int yPos;
private int radius;
private int left;
private int right;
private int top;
private int bottom;
private int color;
public MainGameThread (SurfaceHolder surfaceHolder,MainGameView mainGameView)
{
super();
this.surfaceHolder = surfaceHolder;
this.mainGameView = mainGameView;
}
public void setRunning(boolean running)
{
this.running = running;
}
@Override
public void run() {
while (running)
{
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder)
{
makeChoose();
mainGameView.onDraw(canvas);
}
}
finally {
if(canvas != null)
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
private void makeChoose()
{
int radius;
rnd = new Random();
switch (rnd.nextInt(2))
{
case 0:
setShapeType("Rectangle");
setColor(Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255)));
setXpos(rnd.nextInt(mainGameView.getWidth()));
setYpos(rnd.nextInt(mainGameView.getHeight()));
setTop(rnd.nextInt(mainGameView.getHeight()));
setBottom(rnd.nextInt(mainGameView.getHeight()));
setLeft(rnd.nextInt(mainGameView.getWidth()));
setRight(rnd.nextInt(mainGameView.getWidth()));
break;
case 1:
setShapeType("Circle");
setColor(Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255)));
setXpos(rnd.nextInt(mainGameView.getWidth()));
setYpos(rnd.nextInt(mainGameView.getHeight()));
radius = rnd.nextInt(mainGameView.getWidth()-getXpos());
setRadius(radius);
break;
// case 2:
// setShapeType("Arc");
// setXpos(rnd.nextInt(mainGameView.getWidth()));
// setYpos(rnd.nextInt(mainGameView.getHeight()));
// break;
}
}
private void setShapeType(String shapeType)
{
this.shapeType = shapeType;
}
private void setColor(int color) {
this.color = color;
}
public int getColor() {
return color;
}
private void setRadius(int radius)
{
this.radius = radius;
}
public int getRadius()
{
return radius;
}
public int getBottom() {
return bottom;
}
public int getLeft() {
return left;
}
public int getRight() {
return right;
}
public int getTop() {
return top;
}
private void setBottom(int bottom) {
this.bottom = bottom;
}
private void setLeft(int left) {
this.left = left;
}
private void setRight(int right) {
this.right = right;
}
private void setTop(int top) {
this.top = top;
}
private void setXpos(int xPos)
{
this.xPos = xPos;
}
public int getXpos()
{
return xPos;
}
private void setYpos(int yPos)
{
this.yPos = yPos;
}
public int getYpos()
{
return yPos;
}
public String getShapeType()
{
return shapeType;
}
}
因此当我用于在UI线程中绘图时,它是有效的。但当我触摸矩形 它就像什么都没有发生(我使用得分,我希望在矩形触摸时更新得分)但是得分没有在屏幕上显示,有时显示自己并且消失得非常快。 以下代码为我的MainGameView.class:
package com.example.komeil.surfaceview_2;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by Komeil on 04/11/2016.
*/
public class MainGameView extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap bmp;
MainGameThread mainGameThread;
private int x;
private Paint paint;
private Paint textWhite;
private int score;
private Rect rect;
private boolean touched;
public MainGameView(Context context) {
super(context);
getHolder().addCallback(this);
mainGameThread = new MainGameThread(getHolder(),this);
setFocusable(true);
paint = new Paint();
textWhite = new Paint();
textWhite.setAntiAlias(true);
textWhite.setColor(Color.WHITE);
textWhite.setTextSize(30);
paint.setAntiAlias(true);
bmp = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mainGameThread.setRunning(true);
mainGameThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mainGameThread.setRunning(false);
while (retry)
{
try {
mainGameThread.join();
retry = false;
}
catch (InterruptedException ex)
{
}
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.GRAY);
if(x<getWidth()-bmp.getWidth())
x+=2;
else
x=0;
canvas.drawBitmap(bmp,x,10,null);
switch (mainGameThread.getShapeType())
{
case "Rectangle":
paint.setColor(mainGameThread.getColor());
canvas.drawRect(mainGameThread.getLeft(),mainGameThread.getTop(),mainGameThread.getRight(),mainGameThread.getBottom(),paint);
break;
case "Circle":
paint.setColor(mainGameThread.getColor());
canvas.drawCircle(mainGameThread.getXpos(),mainGameThread.getYpos(),mainGameThread.getRadius(),paint);
break;
// case "Arc":
// paint.setColor(mainGameThread.getColor());
// canvas.drawArc(mainGameThread.getLeft(),mainGameThread.getRight()
// ,mainGameThread.getTop(),mainGameThread.getBottom(),);
// break;
}
if(touched)
{
canvas.drawText(String.valueOf(score),getWidth()/2,50,textWhite);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int eventAction = event.getAction();
int xEvent = (int)event.getX();
int yEvent = (int)event.getY();
switch (eventAction)
{
case MotionEvent.ACTION_DOWN:
switch (mainGameThread.getShapeType())
{
case "Rectangle":
rect = new Rect(mainGameThread.getLeft(),mainGameThread.getTop(),mainGameThread.getRight(),mainGameThread.getBottom());
if(rect.contains(xEvent,yEvent))
{
touched = true;
score +=1;
}
else
{
touched = false;
}
break;
case "Circle":
break;
case "Arc":
break;
}
break;
case MotionEvent.ACTION_UP:
touched = false;
break;
}
return super.onTouchEvent(event);
}
}
那么我如何能够得分并且我需要另一个得分线程吗?