我想爲Android設備製作一個小遊戲。 所有的作品...除了與玩家和對象的碰撞。我做錯什麼了嗎?我已經嘗試了很多檢查碰撞的方法。例如,相交,相交,包含和自我碰撞測試的功能。Android Rect碰撞
編輯:我的問題是,什麼也沒發生:)
所有的DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
private Bitmap player, enemy;
private int speedrun=1, x= 0, y = height - height/5, i=1, yg=height - height/5 + 40, xe= 920, xp =width/2 - width/4;
private Rect p, e;
public static int speed=0;
//other code
@Override
protected void onDraw(Canvas c)
{
c.drawColor(Color.CYAN);
handelPlayer();
p = new Rect(xp, y, 0, 0);
wayrect = new Rect(x, yg, 0, 0);
wayrect2 = new Rect(x + width, yg, 0, 0);
e = new Rect(xe, yg - 250, 0, 0);
c.drawBitmap(enemy, e.left, e.top, null);
c.drawBitmap(player, p.left, p.top, null);
}
public void handelPlayer()
{
x -= speed*speedrun;
xe -= speed*speedrun;
if (x + width < 0)
x = 0;
if (xe < -100)
xe = 920;
if (MainActivity.touch == 1)
{
y -= 100;//jump
MainActivity.touch = 0;
i = 1;
}
if (y <= height - height/5)
y += 3 * i/10; //gravity
i++;
if (p.intersect(e)) //collosision
speedrun = 0;
}
出了什麼問題? –
嘗試將'p.intersect(e)'改爲'Rect.intersects(p,e)'。 'intersect'會改變矩形'p'到交叉點,而'Rect.intersects(Rect a,Rect b)'只是一個測試,我認爲你正在尋找。 –
記得如果你覺得它有用,就可以對你接受的答案進行投票表決;) – Agostino