我有一个简单的问题 - 我试图阻止拖动演员,但我看到奇怪的效果 - 当我试图拖动演员时,它开始眨眼并随机改变位置。这是一段简短的视频:https://youtu.be/KpGujQ7KHc0
演员很简单:
public class Item extends Actor {
private String id;
private TextureRegion texture;
public Item() {
this.id = "passport";
texture = PicturesData.getPicture(getId());
setSize(texture.getRegionWidth(), texture.getRegionHeight());
addListener(new DragListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
Log.add("Dragged: " + x + ", " + y);
setPosition(x, y);
}
});
}
public String getId() {
return id;
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture, getX(), getY());
}
}
屏幕也是基本的:
public class GameScreen implements Screen {
private Stage stage;
public GameScreen() {
stage = new Stage(new StretchViewport(800, 600));
Gdx.input.setInputProcessor(stage);
Item item = new Item();
stage.addActor(item);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
我不知道为什么会这样,你能帮帮我吗?
答案 0 :(得分:1)
Pasha,你的DragListener没有任何问题。使用此项目(硬编码的起始位置):
public class Item extends Image {
private String id;
public Item(String id) {
super(PicturesData.getPicture(getId());
this.id = id;
setPosition(50, 50);
addListener(new DragListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
moveBy(x - getWidth()/2, y - getHeight()/2);
}
});
}
public String getId() {
return id;
}
}
答案 1 :(得分:0)
我认为DragListener
无法正常工作的原因是因为您已覆盖了侦听器正常运行所需的touchDown
函数。只需将您的代码更改为:
addListener(new DragListener() {
@Override
public void drag(InputEvent event, float x, float y, int pointer) {
Log.add("Dragged: " + x + ", " + y);
setPosition(x, y);
}
});