我正在制作Pong游戏,当我点击屏幕时,划桨跳到我的光标点。我想要我需要拖动光标移动他,而不像正常的Pong游戏那样跳跃。我怎么能这样做?
这是我的Paddle课程:
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() {
}
答案 0 :(得分:1)
您正在阅读触摸位置:
Gdx.input.getY()
并直接使用它来设置打击垫位置 - 你不能这样做。
您应该使用InputLister来获取事件。
首先你应该听touchDown,看看是否有用户触摸你的打击垫(比较触摸坐标与打击垫坐标)
然后,对于拖动,您应该使用touchDragged()事件...来在拖动发生时更新打击垫位置,但仅当touchDown检测到触摸时: