【问题标题】:Sprite bouncing off the sides - bottom & right side work but not top and left精灵从侧面反弹 - 底部和右侧工作,但不是顶部和左侧
【发布时间】:2015-05-03 15:07:03
【问题描述】:

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

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

【问题讨论】:

  • 精灵只是来回摆动

标签: android sprite


【解决方案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.

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

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

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

我不知道你实际上想要实现什么,因为你的代码有一半试图包裹一个项目,而另一半试图让它反弹。

【讨论】: