Java 如何使乒乓球比赛中的球拍不跳跃?

Java 如何使乒乓球比赛中的球拍不跳跃?,java,android,libgdx,android-studio-3.0,pong,Java,Android,Libgdx,Android Studio 3.0,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 = widt

我正在做一个乒乓球游戏,当我点击屏幕时,桨跳到我的光标点。我想我需要拖动光标来移动他,而不是像普通的乒乓球游戏那样跳跃。我该怎么做

这是我的划桨课:

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来获取事件

首先,你们应该听触地,看看用户是在触摸你们的键盘,还是并没有比较触摸坐标和键盘坐标

然后,对于拖动,您应该使用touchDragged event…在发生拖动时更新焊盘位置,但仅当触地检测到该触摸时:


你是说你想让用户点击并拖动拨片,还是说你想让拨片滚动到用户点击的地方?我想让用户点击并拖动拨片我该怎么做?在你的类中实现InputListener接口,将你的代码添加到我上面提到的两个方法中。在触摸屏中,只需检查触摸屏上是否有触摸。如果将某个变量设置为TRUE,则移动刚刚开始。当用户抬起手指时,您还可以实现toucup方法将同一变量设置为false。然后在触摸屏中检查此变量是否为真,以及它是否为作为参数获得的值调整键盘坐标。我需要将InputListener实现到我的桨类或我的PlayState类?如何组织代码取决于您。您希望接收输入事件的任何位置。当您实现这个接口时,不要忘记注册您的侦听器。只要用你的对象调用某个方法,将侦听器实现为一个参数-检查一些文档/教程。请将我的答案标记为“正在解决”。