我正在制作一个乒乓球游戏,当我点击屏幕时,桨跳到我的光标点。我希望我需要拖动光标来移动他,而不是像普通的乒乓球游戏那样跳跃。我该怎么做?
这是我的桨课:
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-