如何让精灵从Android游戏的边缘反弹?

时间:2016-05-09 17:26:11

标签: java android sprite

到目前为止,我已经尝试扭转速度,所以他们理论上应该重新开始,但事实并非如此。

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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