Java 无法使事件在我的libgdx Actor中工作
在libgdx中,我很难让事件与我的Java 无法使事件在我的libgdx Actor中工作,java,android,libgdx,Java,Android,Libgdx,在libgdx中,我很难让事件与我的Actor一起工作。我正在使用夜间构建 My stage是在屏幕的子类的show()方法中设置的: stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); Gdx.input.setInputProcessor(stage); TestActor actor = new TestActor(); stage.addActor(actor); 我的演员课看起来
Actor
一起工作。我正在使用夜间构建
My stage是在屏幕的子类的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());
}
}
事件似乎没有发生。奇怪的是,我使用了内置的UI小部件,比如TextButton
,可以很好地触发这些事件。有人能看出我做错了什么吗?你也应该给你的演员一些挫折。
最好的方法(如果您希望与纹理大小相同)
将以下行添加到构造函数:
setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());
请注意,您还可以使用前两个参数设置边界的位置。是!非常感谢。我想知道它是否有类似的东西。