我正在制作一款安卓游戏。游戏是在随机时间间隔内随机出现在屏幕上不同位置的痣。
问题是我不知道如何让痣保持可见几秒钟。我wait(1000)
在 的持有人上使用过OnDraw()
,但这次它成为了游戏堆栈。我试图改SurfaceView
回View
,但随后屏幕停止刷新。
有什么建议吗?
主要活动类
public class First_Stage extends Activity implements OnTouchListener{
private AllViews allViews;
private MoleView moleView;
private PointsView pointsView;
private TimerView timerView;
private StageView stageView;
private Mole mole;
private MoveMole moleMove;
private int points=0;
private StagePoints stagePoints;
private PointsSingelton poin;
private float x,y;
private Club club;
private ClubView clubView;
private PointsSingelton pointsCount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
club=new Club();
clubView = new ClubView(this, club);
mole=new Mole();
stageView=new StageView(this);
moleView=new MoleView(this,mole);
pointsView=new PointsView(this);
timerView=new TimerView(this, "3:33");
allViews=new AllViews(this);
allViews.setViews(stageView, moleView, pointsView, timerView,clubView);
setContentView(allViews);
allViews.setOnTouchListener((View.OnTouchListener)this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
x=event.getX();
y=event.getY();
moleView.setX(x);
moleView.setY(y);
allViews.setX(x);
allViews.setY(y);
if ((x<100 && x>0)&&(y>0&&y<100)){
points=pointsCount.getInstance().nextPoint();
pointsView.setPoint(points);
moleView.setBool(true);
}
return true;
}
所有视图类
public class AllViews extends SurfaceView implements SurfaceHolder.Callback,Runnable {
private Club club;
private ClubView clubView;
private MoleView moleView;
private PointsView pointsView;
private TimerView timerView;
private StageView mainView;
private float x, y;
private Paint test;
private First_Stage first;
Thread drawThread = new Thread(this);
SurfaceHolder holder;
private Bitmap clubPic;
public AllViews(Context context) {
super(context);
test = new Paint();
first = new First_Stage();
holder = getHolder();
holder.addCallback(this);
}
public void setViews(StageView mainView, MoleView moleView,
PointsView pointsView, TimerView timerView,ClubView clubView)
{
this.mainView = mainView;
this.moleView = moleView;
this.pointsView = pointsView;
this.timerView = timerView;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mainView.onDraw(canvas);
moleView.onDraw(canvas);
pointsView.onDraw(canvas);
timerView.onDraw(canvas);
clubPic=BitmapFactory.decodeResource(getResources(), R.drawable.clubdown);
canvas.drawBitmap(clubPic, this.x-39,this.y-20, null);
}
@Override
public void run() {
Canvas c;
while (true) {
c = null;
try {
c = holder.lockCanvas(null);
synchronized (holder) {
onDraw(c);
}
} finally {
if (c != null) {
holder.unlockCanvasAndPost(c);
}
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawThread.start();
}
鼹鼠视图类
public class MoleView extends View {
private Mole mole;
private Bitmap molePic;
private float x,y;
private boolean bool=false;
public MoleView(Context context, Mole mole) {
super(context);
this.mole=mole;
}
public boolean isBamped(){
float xmin=mole.getX();
float xmax=mole.getX()+60;
float ymin=mole.getY();
float ymax=mole.getY()+46;
if ((this.x<xmax&&this.x>xmin)&&(this.y<ymax&&this.y>ymin)){
return true;
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if (!bool){
molePic=BitmapFactory.decodeResource(getResources(), R.drawable.nest_full_mole);
canvas.drawBitmap(molePic, mole.getX(), mole.getY(), null);
mole.moveMole();
}else {
molePic=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(molePic, mole.getX(), mole.getY(), null);
molePic.recycle();
}
}
鼹鼠类
public class Mole {
private float x;
private float y;
private Positions myPositions;
public Mole() {
super();
myPositions=new Positions();
this.x = myPositions.getRandomX();
this.y = myPositions.getRandomY();
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}