问:Android-如何检测线程的冲突

时间:2015-11-14 15:47:01

标签: java android

我希望能够检测到在我的活动中使用线程的类的碰撞。当检测到碰撞时,应该启动新活动。在我的情况下,只有在检测到碰撞时我才会启动新活动。 screen.I希望它立即开始新活动而不必触摸它。我认为问题出在update()方法(在活动类中)。我在错误的地方调用它。有什么建议吗?

线程类

private boolean Collision;
Thread t=null;
Pravougaonik pr;
SurfaceHolder holder;
boolean isRunning=false;
SnakeParts sp;
ZmijicaCrtanje zc;
public ZmijicaSV(Context context) {
    super(context);
    holder=getHolder();
    pr=new Pravougaonik(context);
    sp=new SnakeParts();
    zc=new ZmijicaCrtanje();
    Collision=false;
}

public void run()
{
    Paint p=new Paint();
    p.setColor(Color.BLUE);
    p.setStyle(Paint.Style.FILL);
     while(isRunning)
     {

         if(!holder.getSurface().isValid()) {
             continue;
         }
         Canvas c=holder.lockCanvas();
         c.drawColor(Color.BLACK);
         sp.Draw(c);
         pr.onDraw(c);
         sp.update();
         drawText(c);
         holder.unlockCanvasAndPost(c);

         try {

            t.sleep(50);

         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
}

public void resume()
{
    isRunning=true;
    t=new Thread(this);
    t.start();
}

public void pause()
{
    isRunning=false;
    while(true)
    {
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        break;
    }
    t=null;

}

public void drawText(Canvas canvas)
{
    Paint paint=new Paint();
    paint.setColor(Color.WHITE);
    paint.setTextSize(50);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
    canvas.drawText("Score:"+zc.getScore(),getWidth()-230,getHeight()-25,paint);
}

public boolean checkCollision()
{
    if((sp.getHeadX()<-1)||(sp.getHeadX()>getWidth())||(sp.getHeadY()>getHeight())||(sp.getHeadY()<-1))
        Collision=true;
    return Collision;
}

}

活动类

ZmijicaSV zmija;
private static int score=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    zmija=new ZmijicaSV(this);
    zmija.setOnTouchListener(this);
    setContentView(zmija);

}

@Override
protected void onPause() {
    super.onPause();
    zmija.pause();
    }


@Override
protected void onResume() {
    super.onResume();
    zmija.resume();

}



@Override
public boolean onTouch(View v, MotionEvent event) {

    boolean up=zmija.sp.isUp();
    boolean right=zmija.sp.isRight();
    boolean down=zmija.sp.isDown();
    boolean left=zmija.sp.isLeft();


    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if(up) {
                zmija.sp.setUp(false);
                zmija.sp.setRight(true);
                update();
                break;
            }

            if(right) {
                zmija.sp.setRight(false);
                zmija.sp.setDown(true);
                update();
                break;
            }

            if(down) {
                zmija.sp.setDown(false);
                zmija.sp.setLeft(true);
                update();
                break;
            }
            if(left) {
                zmija.sp.setLeft(false);
                zmija.sp.setUp(true);
                update();
            break;
        }


    }
        return true;
    }

public void update()
{
    score++;
    if(zmija.checkCollision())
        startActivity(new Intent(ZmijicaCrtanje.this, GameOver.class));
}
public int getScore()
{
    return score;
}

}

0 个答案:

没有答案