2

这是让我感到困惑的代码。我可能在这里遗漏了一些东西,但无法弄清楚。

public class TStage extends Stage {

    public TStage(float width, float height, boolean stretch) {
        super(width, height, stretch);
    }

    @Override
    public Actor hit(float x, float y) {
        Gdx.app.debug("HUNT", "in hit of TStage");
        return super.hit(x, y);
    }
}

public class TActor extends Actor {

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        // draw something here
    }

    @Override
    public Actor hit(float x, float y) {
        Gdx.app.debug("HUNT", "in hit of TActor");
        return null;
    }
}

    /* Code to set stage*/

    TStage stage = new TStage(Hunt.GAME_WIDTH, Hunt.GAME_HEIGHT, false);
    Gdx.input.setInputProcessor(stage);
    TActor actor1 = new TActor();
    stage.addActor(tactor);

当我触摸屏幕

输出:

in hit of TActor  

我所期待的:

in hit of TStage  
in hit of TActor 

[编辑]
我将以下代码添加到 TStage 类

@Override
    public Actor touchDown(int x, int y, int pointer, int button) {
        Gdx.app.debug("HUNT", "in touchDown of TStage");
        return super.touchDown(x, y, pointer, button);
    }

现在输出是:

in touchDown of TStage  
in hit of TActor 
4

1 回答 1

3

关于哪种方法做什么有些困惑。

该方法hit()返回位于这些坐标的演员。你想要的方法是touchDown()。javadocs 中几乎没有任何信息,因此请在此处阅读。你会看到它TActor.hit()被调用了,因为这就是在那些坐标处Stage.touchDown()找到的方法。Actor

于 2012-06-19T02:13:37.877 回答