gpt4 book ai didi

java - Lidbgx 从右向左移动对象

转载 作者:行者123 更新时间:2023-12-01 23:30:48 24 4
gpt4 key购买 nike

batch.begin();

batch.draw(tr_background, 0, 0, 3024, 1443);

batch.draw(tr_ball, x, 110, 100, 100);

batch.end();

x = x + 100 * Gdx.graphics.getDeltaTime();

if(x > camera.viewportWidth)
x = -100;

它从左向右移动,然后回到起始位置。当tr_ball到达右侧时如何将其从右向左移动?

最佳答案

为类(class)创建一个球速变量

float ballSpeed = 100;

然后当球越过屏幕末端时翻转它。因此,替换上面 batch.end() 之后的代码:

x += ballSpeed * Gdx.graphics.getDeltaTime();

if (x >= camera.viewportWidth - 100) {
x = camera.viewportWidth; // prevent overshooting
ballSpeed *= -1;
} else if (x <= 0) {
x = 0; // prevent overshooting
ballSpeed *= -1;
}

此代码假设您的相机位于屏幕左边缘,位置为 0。 100 也是球的宽度。这实际上应该是一个常量(静态最终 float ),因为您将在代码中的多个位置使用它。

关于java - Lidbgx 从右向左移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com