Android - 无法让鼹鼠弹出

时间:2012-10-15 21:10:07

标签: java android surfaceview

我正在制作Android游戏。游戏是随机持续时间以随机间隔出现在屏幕上不同位置的鼹鼠。

问题是我不知道如何让鼹鼠在几秒钟内保持可见状态。我在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();

}

moleView类

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();
    }


}

Mole Class

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;
}
}

1 个答案:

答案 0 :(得分:1)

我无法在代码中找到你希望将它保留在屏幕上的位置,因为仍然有很多代码但是尝试这样:

在绘制痣的类中创建一个新的Handler。之后,您可以调用postDelayed()函数,使用该函数可以在定义的时间段内执行某些操作。事实上非常简单。

链接: http://developer.android.com/reference/android/os/Handler.html

http://www.vogella.com/articles/AndroidPerformance/article.html