如何在没有按钮点击android的情况下从另一个活动开始新活动

时间:2015-11-13 20:58:21

标签: java android

我制作了一个简单的游戏,每当我的物体到达屏幕的末端时,当前的活动就会停止,另一个活动会响起。

我创建了一个名为collisionDetection()的简单布尔方法,用于检查对象是否正在触及屏幕的末尾。但是,我不知道应该在哪里使用该方法collisionDetection()

我应该检查onResume上的碰撞吗?当检测到碰撞时,如何启动另一个活动并停止主活动?

第一项活动

的课程
public class ZmijicaCrtanje extends AppCompatActivity implements View.OnTouchListener{

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++;
}
public int getScore()
{
    return score;
}

}

公共类ZmijicaSV扩展了SurfaceView实现的Runnable {

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

}

1 个答案:

答案 0 :(得分:0)

collisionDetection()方法放在public void update()方法中,如

 public void update()
 {
   score++;
   collisionDetection();
 }

collisionDetection()通过意图开始下一个活动时检测到匹配

Intent next=new Intent(this.class,your_class_for_secondactivity.class);
startActivity(next);