如何停止运球?
How to stop the dribbling?
我正在使用 libGDX 制作一个新的突破游戏(我是 libGDX 的新手)并且在游戏中,每当球接触球拍时它就会开始运球而不在球拍上弹跳.
我已经尝试在这个游戏中改变球的 ySpeed。
这是我的球的代码 class。
package com.thejavabay.my_game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Intersector;
public class Ball{
int x;
int y;
int size;
int xSpeed;
int ySpeed;
Circle cic = new Circle();
Color color = Color.WHITE;
public Ball(int x, int y, int size, int xSpeed, int ySpeed) {
this.x = x;
this.y = y;
this.size = size;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
public void update() {
x += xSpeed;
y += ySpeed;
if (x < size || x > Gdx.graphics.getWidth() - size)
xSpeed = -xSpeed;
if (y < size || y > Gdx.graphics.getHeight() - size)
ySpeed = -ySpeed;
}
public void draw(ShapeRenderer shape) {
cic.x = x;
cic.y = y;
cic.radius = size;
shape.setColor(color);
shape.circle(x, y, size);
shape.circle(x, y, size);
}
private static boolean collidesWith(Paddle paddle, Ball ball) {
if(Intersector.overlaps(ball.cic, paddle.rect))
return true;
else
return false;
}
public void checkCollision(Paddle paddle, Ball ball) {
if(collidesWith(paddle, ball)) {
ySpeed = -ySpeed;
}
}
}
我预计球会从球拍上弹开,但它一直在球拍上移动。
如果你的执行顺序是
paint()->update()->checkCollision() 或 paint()->checkCollision->update()
因此,在第一次碰撞后,每三次碰撞检查都会 return 为真,并且你的速度会乒乓
并且因为你在绘画中有 circle.x 它会每 2 帧恢复到原始位置..固定更新应该如下所示..但在看之前请自己解决。
public void update(){
x += xSpeed;
y += ySpeed;
if (x < size || x > Gdx.graphics.getWidth() - size)
xSpeed = -xSpeed;
if (y < size || y > Gdx.graphics.getHeight() - size)
ySpeed = -ySpeed;
cic.x = x;
cic.y = y;
cic.radius = size;
}
我正在使用 libGDX 制作一个新的突破游戏(我是 libGDX 的新手)并且在游戏中,每当球接触球拍时它就会开始运球而不在球拍上弹跳.
我已经尝试在这个游戏中改变球的 ySpeed。
这是我的球的代码 class。
package com.thejavabay.my_game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Intersector;
public class Ball{
int x;
int y;
int size;
int xSpeed;
int ySpeed;
Circle cic = new Circle();
Color color = Color.WHITE;
public Ball(int x, int y, int size, int xSpeed, int ySpeed) {
this.x = x;
this.y = y;
this.size = size;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
public void update() {
x += xSpeed;
y += ySpeed;
if (x < size || x > Gdx.graphics.getWidth() - size)
xSpeed = -xSpeed;
if (y < size || y > Gdx.graphics.getHeight() - size)
ySpeed = -ySpeed;
}
public void draw(ShapeRenderer shape) {
cic.x = x;
cic.y = y;
cic.radius = size;
shape.setColor(color);
shape.circle(x, y, size);
shape.circle(x, y, size);
}
private static boolean collidesWith(Paddle paddle, Ball ball) {
if(Intersector.overlaps(ball.cic, paddle.rect))
return true;
else
return false;
}
public void checkCollision(Paddle paddle, Ball ball) {
if(collidesWith(paddle, ball)) {
ySpeed = -ySpeed;
}
}
}
我预计球会从球拍上弹开,但它一直在球拍上移动。
如果你的执行顺序是 paint()->update()->checkCollision() 或 paint()->checkCollision->update()
因此,在第一次碰撞后,每三次碰撞检查都会 return 为真,并且你的速度会乒乓
并且因为你在绘画中有 circle.x 它会每 2 帧恢复到原始位置..固定更新应该如下所示..但在看之前请自己解决。
public void update(){
x += xSpeed;
y += ySpeed;
if (x < size || x > Gdx.graphics.getWidth() - size)
xSpeed = -xSpeed;
if (y < size || y > Gdx.graphics.getHeight() - size)
ySpeed = -ySpeed;
cic.x = x;
cic.y = y;
cic.radius = size;
}