2

我这样定义了新的演员:

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

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

4

1 回答 1

5

你必须 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());

请注意,我将边界位置设置为 200 , 200 ,因为您在那里绘制

于 2013-02-21T08:53:07.293 回答