当用户单击图像时,有没有办法将图像移动到不同的位置。
public Players() {
deck.displayHand();
stage = new Stage(new ScreenViewport());
Image card1 = new Image(sprite);
card1.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
stage.act();
stage.draw();
}
我知道我必须添加一个侦听器,但我很困惑之后该怎么做。目标是让用户触摸卡片图像,它将移动到不同的位置。
-
当您/用户单击该图像时移动到预定义的位置:
向该图像添加动作(演员子级(, 假设预定义的位置是
x_pos
,y_pos
和移动时间t
秒card1.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { card1.addAction(Actions.moveTo(x_pos, y_pos,t)); } });
-
目标是触摸卡片/图像,它会根据用户的触摸移动到不同的位置:
card1.addListener(new DragListener(){ @Override public void drag(InputEvent event, float x, float y, int pointer) { card1.moveBy(x - card1.getWidth() / 2, y - card1.getHeight() / 2); super.drag(event, x, y, pointer); } });
您需要将舞台设置为InputProcessor
Gdx.input.setInputProcessor(stage);
Image
是一个Actor
,因此您可以向其添加操作。
card1.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
card1.addAction(Actions.moveTo(100, 100, .5f, Interpolation.circleIn));
}
});