1

位图 A 的位置是其 X/Y 位置是静态的,位图 B 是移动位图,它从屏幕底部移动到用户在屏幕上触摸的任何位置。当移动位图到达静态位图但它不被识别为碰撞时,移动位图使用浮点数,我四舍五入,静态位图使用 int。下面是一段代码,我想弄清楚我的位图交叉路径需要更改哪些内容才能被视为碰撞

                  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;

任何提示将不胜感激,谢谢。

4

1 回答 1

0

抱歉,如果这是一个愚蠢的答案,但您的支票应该是:

if(canvas.getHeight() - Math.round(animY*-1) >= (canvas.getHeight() - (canvas.getHeight()-32))){

反而?(将“==”更改为“>=”)否则您只检查岩石何时完全落在(canvas.getheight -32)上,而不是检查过去

于 2012-11-06T19:37:01.817 回答