2012-09-19 26 views
13

我很難在libgdx中使用我的Actor處理事件。我正在使用每晚構建。無法在我的libgdx Actor中獲取事件

我的舞臺是建立在Screen子類的show()方法:

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

事件似乎並不火。奇怪的是,我已經使用了像TextButton這樣的內置UI小部件,並且可以讓這些事件觸發得很好。任何人都可以看到我在做什麼錯了嗎?

回答

16

您還應該爲您的演員設置Bounds。 最好的辦法做到這一點(如果你想要的大小爲你的紋理相同) 這些行添加到您的構造函數:

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

通知您也可以與第一2點的參數設置邊界的位置。

+0

是的!謝謝!我想知道它是否有這方面的內容。 –

相關問題