13

Actorlibgdxでイベントを動作させるのに苦労しています。私はナイトリービルドを使用しています。

私のステージは、サブクラスのshow()メソッドで設定されています:Screen

stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
Gdx.input.setInputProcessor(stage);  
TestActor actor = new TestActor(); 
stage.addActor(actor);

私のアクタークラスは次のようになります。

class TestActor extends Actor {
    private Sprite sprite;
    private TextureAtlas atlas; 

    public TestActor() {   
        atlas = new TextureAtlas(Gdx.files.internal("textures/images-packed.atlas"));
        sprite = atlas.createSprite("logo-96");

        setTouchable(Touchable.enabled);
        addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.debug(TestGame.TAG, "TestActor.touchDown()");
                return true;  // must return true for touchUp event to occur
            }
            public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.debug(TestGame.TAG, "TestActor.touchUp()");
            }
        });
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(sprite, getX(), getY());
    }            
}

イベントは発生していないようです。奇妙なことに、私は のような組み込みの UI ウィジェットを使用してTextButtonおり、それらのイベントを正常に起動させることができます。誰かが私が間違っていることを見ることができますか?

4

1 に答える 1

16

また、アクターに Bounds を設定する必要があります。それを行う最善の方法 (テクスチャと同じサイズが必要な場合) は、次の行をコンストラクターに追加します。

setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());

最初の 2 つのパラメーターを使用して境界の位置を設定することもできます。

于 2012-09-19T14:20:18.260 に答える