位圖A具有位置這是它的X/Y位置是靜態的,位圖B被移動的位圖,其從屏幕的底部移動到在以往任何時候用戶觸摸屏幕上。當移動位圖到達靜態位圖但是它不會被識別爲衝突時,移動位圖使用浮點數,而我使用的是浮點數,靜態位圖使用int。下面的代碼片段,我想弄清楚哪些是需要改變我的位圖跨越的路徑被認爲是一個碰撞問題與2D碰撞位圖
canvas.drawBitmap(rock2, 70, 32, null);
//this is where it checks to see if the moving bitmaps y coordinate is matched to the Y coordinate of the static bitmap
if(canvas.getHeight() - Math.round(animY*-1) == (canvas.getHeight() - (canvas.getHeight()-32))){
Log.d("ALERT", "COLLIDE");
}
//the Y value is *-1 because the moving bitmap is starting from the bottom of the screen so this converts the value to positive value
我不知道如果我的計算是關閉,或者我要對這個碰撞錯誤的方法。以下是我的移動代碼片段
//UP Y ANIMATION
if(animY*-1 < canvas.getHeight() && !bumpY){
animY += speedY;
//IF ROCK IS NOT AT EXTREME LEFT OR EXTREME RIGHT KEEP ON TRACK
if(animX < (canvas.getWidth()/2) || animX*-1 < (canvas.getWidth()/2) && !bumpX){
animX += speedX;
//IF ROCK HITS EDGE LEFT/RIGHT RETURN TO SENDER (INDICATE BUMP)
if(animX*-1 > (canvas.getWidth()/2) - rock.getWidth()/2 || animX > (canvas.getWidth()/2) - rock.getWidth()/2){
bumpX = true;
bumpY = true;
}
}
//IF Y HITS TOP OF SCREEN
if(animY*-1 > canvas.getHeight()){
bumpY = true;
}
}
//DOWN Y ANIMATION
if(animY < 0 && bumpY){
//REVERSE DIRECTION OF Y
animY -= speedY;
//IF ROCK HITS TOP OR SIDE REVERSE X DIRECTION
if(bumpX || bumpY)
animX -= speedX;
//IF AT STARTING POINT
if(animY > 0){
bumpY = false;
bumpX = false;
}
}
//in an ontouch method where the X and Y values are calculated
case MotionEvent.ACTION_UP:
finalX = event.getX() - (cross.getWidth()/2);
finalY = event.getY() - (cross.getHeight()/2);
moveToX = finalX - startX;
moveToY = finalY - startY;
speedX = moveToX/50;
speedY = moveToY/50;
break;
任何提示將不勝感激,謝謝。
中有你Log.d信息添加到在時間方面顯示岩石的座標是什麼?不能確定,但我認爲你的計算方式「if(canvas.getHeight() - Math.round(animY * -1)==(canvas.getHeight() - (canvas.getHeight( )-32)))「...並且打印出來,看看每個步驟的結果是什麼 –
移動位圖不會以完美的順序移動,我也記錄了移動。根據從起始點開始,點擊碰撞檢測並登錄貓,如果你觸及距離起始點更近的方法,我用來計算工程,但距離更遠,但它沒有。也許是一個舍入問題?如果是這樣,我需要一些方向大聲笑。感謝您的評論 – kabuto178