作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
batch.begin(); batch.draw(tr_background, 0, 0, 3024, 1443); batch.draw(tr_ball, x, 110, 100,
我是一名优秀的程序员,十分优秀!