gpt4 book ai didi

java - touchDragged 的​​奇怪行为

转载 作者:行者123 更新时间:2023-11-30 07:21:22 26 4
gpt4 key购买 nike

我有一个简单的问题 - 我正在尝试对 Actor 进行拖动,但我看到了奇怪的效果 - 当我尝试拖动 Actor 时,它开始闪烁并随机改变位置。这是一个短视频:https://youtu.be/KpGujQ7KHc0

Actor 很简单:

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() {

}
}

我不知道为什么会这样,你能帮我吗?

最佳答案

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;
}
}

关于java - touchDragged 的​​奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com