到目前为止,我已经尝试过使速度反向,因此从理论上讲它们应该回到自己身上,但事实并非如此。

 public void update() {
    if (x > gameView.getWidth() - bmp_width - xSpeed){
        xSpeed = xSpeed * -1;
        ySpeed = ySpeed * -1;
    }
    if (y > gameView.getHeight() - bmp_height - ySpeed){
        xSpeed = xSpeed * -1;;
        ySpeed = ySpeed * -1;;
    }
    if(x + xSpeed < 0){
        x = 0;
        xSpeed = 0;
        ySpeed = -5;
    }

    if(y + ySpeed < 0){
        y = 0;
        xSpeed = 5;
        ySpeed = 0;
    }
    x = x + xSpeed;
    y = y + ySpeed;
}


提前致谢!

最佳答案

尝试这样的事情:

public void update() {
    x += xSpeed;
    y += ySpeed;

    if (x >= gameView.getWidth() - bmp_width){
        xSpeed = -xSpeed;
        x = gameView.getWidth() - bmp_width;
    }else if(x <= 0){
        xSpeed = -xSpeed;
        x = 0;
    }
    if (y >= gameView.getHeight() - bmp_height){
        ySpeed = -ySpeed;
        y = gameView.getHeight() - bmp_height;
    }else if(y <= 0){
        ySpeed = -ySpeed;
        y = 0;
    }
}

10-04 23:38