精灵从两侧弹跳 - 底部和右侧有效但顶部和左侧无效

Sprite bouncing off the sides - bottom & right side work but not top and left

我正在尝试创建一个环绕方法来使屏幕每一侧的精灵反弹。我的底部和右侧工作正常,但似乎无法弄清楚为什么它不会离开顶部或左侧。任何帮助将非常感激。这是我的代码:

public void wrapAround(){
    wrapAround = true;
    //Code to wrap around   
    if (x < 0) x = x + gameView.getWidth(); //increment x whilst not off screen
    if (x >= gameView.getWidth()){ //if gone of the right sides of screen

        xSpeed = (xSpeed * -1);
    }
    if (x <= gameView.getWidth())
    {
        xSpeed = (xSpeed * -1);
    }

    if (y < 0) y = y + gameView.getHeight();//increment y whilst not off screen
    if (y >= gameView.getHeight()){//if gone of the bottom of screen


        ySpeed = (ySpeed * -1);
    }
    if (y <= gameView.getHeight())
    {
        ySpeed = (ySpeed * -1);
    }

您的问题需要大量澄清。

if (x < 0) x = x + gameView.getWidth(); //this code executes only when x IS off screen on the left side, which means you want to move it to the right size. No bouncing is implied here.

如果精灵在右侧离开屏幕,下面的命令会反转精灵的速度,但是如果之后再次调用环绕函数,速度会再次反转,使其在向左击中时来回摆动或底墙。

if (x >= gameView.getWidth()){ //if gone of the right sides of screen

    xSpeed = (xSpeed * -1);
}
if (x <= gameView.getWidth())
{
    xSpeed = (xSpeed * -1);
}

我不知道你到底想达到什么目的,因为你的一半代码试图包裹一个项目,而另一半试图让它反弹。