java - touchDragged 的​​奇怪行为

标签 java libgdx

我有一个简单的问题 - 我正在尝试对 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/

相关文章:

java - 带集合的 MultiMap<String>

java - 如何使用增强的 for 循环初始化数组?

java - JPA:返回多个实体的查询

java - 应该为游戏中的所有图像制作一个大纹理图集,还是为每个屏幕制作一个纹理图集?

libgdx - libgdx 中的纹理打包器大小问题

ios - LibGDX 应用因不包含 NSCalendarsUsageDescription 而被 AppStore 拒绝

java - 此请求的时间戳在 recvWindow 之外

java - <上下文:componentscan> how does it work?

java - 如何更新 Libgdx 中的表行?

java - libGDX 游戏在 iOS 7 上崩溃