我正在制作乒乓球游戏,当我单击屏幕时, Racket 会跳到我的光标点。我希望我需要拖动光标来移动他,而不是像普通的乒乓球游戏那样跳跃。我该怎么做?
这是我的 Paddle 类(class):
public class Paddle {
private Vector3 position;
private int width, height;
private Texture texture;
public Paddle(int x, int y, int width, int height){
this.width = width;
this.height = height;
createTexture(width,height);
position = new Vector3(x, y, 0);
}
private void createTexture(int width, int height) {
Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.BLACK);
pixmap.fillRectangle(0, 0, width, height);
texture = new Texture(pixmap);
pixmap.dispose();
}
public void update(int y){
position.add(0, y - position.y,0);
position.y = y;
position.set(position.x, HeadGuns.HEIGHT - position.y, position.z);
}
public void draw(SpriteBatch sb){
sb.draw(texture, position.x, position.y, width,height);
}
这是我的 PlayState 类:
public class PlayState extends State {
private Paddle myPaddle;
public PlayState(GameStateManager gsm) {
super(gsm);
myPaddle = new Paddle(25, HeadGuns.HEIGHT/2, 25, 150);
}
@Override
public void handleInput() {
if (Gdx.input.isTouched()){
//when I touched the screen
myPaddle.update(Gdx.input.getY());
}
}
@Override
public void update(float dt) {
handleInput();
}
@Override
public void render(SpriteBatch sb) {
sb.begin();
myPaddle.draw(sb);
sb.end();
}
@Override
public void dispose() {
}
最佳答案
您正在阅读触摸位置:
Gdx.input.getY()
并直接使用它来设置焊盘位置 - 你不能这样做。
您应该使用InputLister来获取事件。
首先,您应该听 touchDown 并查看用户是否触摸您的触摸板(比较触摸坐标与触摸板坐标)
然后,对于拖动,您应该使用 touchDragged() 事件...在拖动发生时更新焊盘位置,但前提是 touchDown 检测到该触摸:
关于java - LIBGDX - 如何使乒乓球游戏中的 Racket 不跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48332011/