LIBGDX - 如何让球拍在乒乓球游戏中不跳跃?

LIBGDX - How to make the paddle not jumping in pong game?

我正在制作乒乓球游戏,当我点击屏幕时球拍跳到我的光标点。我希望我需要拖动光标来移动他,而不像普通的 Pong 游戏那样跳跃。我该怎么做?

这是我的桨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 class:

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 检测到该触摸:

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/InputListener.html#touchDragged-com.badlogic.gdx.scenes.scene2d.InputEvent-float-float-int-