java - Android - 无法让痣弹出

标签 java android surfaceview

<分区>

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

问题是我不知道如何让痣保持可见几秒钟。我在OnDraw() 的holder 上使用了wait(1000),但这次它使游戏堆栈。我试图将 SurfaceView 改回 View,但随后屏幕停止刷新。

有什么建议吗?

主要 Activity 类

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

所有 View 类

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


}

鼹鼠类

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

最佳答案

我真的无法在代码中找到您想将其保留在屏幕上的位置,因为仍然有很多代码,但请像这样尝试:

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

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

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

关于java - Android - 无法让痣弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904049/

相关文章:

java - 使用传输编码改造客户端和响应 : chunked

java - Retrofit 中未正确处理 REST 方法中的参数

java - REST 应用程序在 Eclipse 中运行,但不是作为可执行 jar。 NoClassDefFound错误: javax/xml/bind/annotation/XmlElement

java - 为什么 Java 不从文件中读取这些行?

android - 发布android库到jcenter : gradle bintrayUpload does not find libraries

android - 位图和 Android 堆

java - Tomcat 6.0.16 取消部署不会删除 MySQL Jar

Android finish() Activity 不工作

android - 将大图设置为 SurfaceView 的背景,使其适合 Android 中的显示高度

android - 在 glThread 上运行长任务而不阻塞 Android 上的 UI 线程