Libgdx Actor不响应输入

Alf*_*Alf 2 java libgdx

我这样定义了新演员:

class MyActor extends Image
{
    Texture texMyActor;

    public MyActor()
    {
        super.setTouchable(Touchable.enabled);
        texMyActor = new Texture("data/myActor.png");
        addListener(new InputListener()
        {
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button)
            {
                System.out.println("down");
                return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                    int pointer, int button)
            {
                System.out.println("up");
            }
        });
    }

    @Override
    public void act(float delta)
    {
        super.act(delta);
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha)
    {
        batch.draw(texMyActor, 200, 200);
    }
}
Run Code Online (Sandbox Code Playgroud)

我还添加了演员到舞台,注册舞台作为当前输入处理器.然后我调用stage.act(deltaTime)和stage.draw().我看到我的演员,但它没有响应输入我的代码有什么问题?:?:

ida*_*kav 5

你必须将你的actor的setBounds添加到你的构造函数中:

texMyActor = new Texture("data/myActor.png");
//setting width and height according to the texture
setWidth(texMyActor.getWidth());
setHeight(texMyActor.getHeight());
//setting bound of the actor
setBounds(200, 200, getWidth(), getHeight());
Run Code Online (Sandbox Code Playgroud)

请注意,自从您在那里绘制以来,我将边界位置设置为200,200