Mic*_*ick 13 java android libgdx
我很难Actor在libgdx中使用我的事件.我正在使用夜间构建.
我的舞台是在子类的show()方法中设置的Screen:
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
Gdx.input.setInputProcessor(stage);
TestActor actor = new TestActor();
stage.addActor(actor);
Run Code Online (Sandbox Code Playgroud)
我的演员班看起来像:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
事件似乎没有发生.奇怪的是,我已经使用了像这样的内置UI小部件,TextButton并且可以使这些事件触发得很好.谁能看到我做错了什么?
ida*_*kav 16
您还应该将setBounds设置为您的actor.最好的方法(如果你想要与纹理相同的大小)将这些行添加到构造函数中:
setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());
Run Code Online (Sandbox Code Playgroud)
请注意,您还可以使用前2个参数设置边界的位置.
| 归档时间: |
|
| 查看次数: |
7435 次 |
| 最近记录: |