我正在做一个Pong游戏,当我在屏幕上单击时,桨将跳到我的光标点。我希望我需要拖动光标来移动他,并且不像普通的Pong游戏那样跳跃。我怎样才能做到这一点?

这是我的桨课程:

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

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

关于java - LIBGDX-如何使 Racket 在乒乓球比赛中不跳跃?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48332011/

10-09 06:23
查看更多