简体   繁体   English

libgdx inputListener不适用于actor

[英]libgdx inputListener doesn't work with actor

I tried use InputListener with Actor, but it doesn't work. 我尝试将InputListener与Actor一起使用,但不起作用。 I really don't know why. 我真的不知道为什么 I found many information about it and saw official documentation but not one of all didn't help me. 我发现了很多有关此的信息,并看到了官方文档,但没有一个没有帮助我。

My log messages are not shown when I touch the sprite. 触摸精灵时,不会显示我的日志消息。 But if I use the global input processor (for whole screen) it works fine, but I want to add only one listener only for actor. 但是,如果我使用全局输入处理器(用于整个屏幕),则可以正常工作,但是我只想为演员添加一个侦听器。

What am I doing wrong? 我究竟做错了什么?

public class GameScreen implements Screen {
    final Launch launch;
    Texture texture;
    Stage stage;


    public GameScreen(Launch launch) {
        Gdx.app.setLogLevel(Application.LOG_DEBUG);
        this.launch = launch;
        texture = new Texture("hero.png");
        stage = new Stage();
        Hero hero = new Hero();
        hero.addListener(new HeroListener());
        stage.addActor(hero);
        Gdx.input.setInputProcessor(stage);

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.draw();
        stage.act(Gdx.graphics.getDeltaTime());

    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        stage.dispose();
        texture.dispose();
    }

    private class Hero extends Actor {

        @Override
        public boolean addListener(EventListener listener) {
            Gdx.app.debug("MyTag", "my debug message");
            return super.addListener(listener);
        }

        @Override
        public void draw(Batch batch, float parentAlpha) {
            batch.draw(texture, 0, 0, 500, 500);
        }
    }

    private class HeroListener extends InputListener {
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.debug("MyTag", "touch down");
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.debug("MyTag", "touch up");
            super.touchUp(event, x, y, pointer, button);
        }
    }
}

The problem is that you are not giving any size or position to your actor. 问题是您没有给演员任何尺寸或位置。 InputListener work for events whitin the bounds of the Actor . InputListener事件工作whitin的的边界Actor If you do not define any size to your Actor , it won't ever receive an event. 如果您没有为Actor定义任何大小,它将永远不会收到事件。

Hero hero = new Hero();
hero.addListener(new HeroListener());

hero.setBounds(0, 0, 500, 500);

stage.addActor(hero);

That should make your InputListener listen to touchDown/Up events in the (0, 0), (500, 500) bounds. 那应该使您的InputListener在(0,0),(500,500)范围内监听touchDown / Up事件。

Also, it's conveninent because you can use those bounds when drawing after: 另外,这很方便,因为您可以在以下绘制时使用这些边界:

@Override
public void draw(Batch batch, float parentAlpha) {
    batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM