这是令我困惑的代码。我可能在这里遗漏了一些东西,但无法弄清楚。
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
最佳答案
对于哪种方法的作用存在一些困惑。
方法hit()
返回位于这些坐标处的 Actor 。您想要的方法是 touchDown()
。 javadocs 中几乎没有任何信息,因此请阅读此 here 。您将看到 TActor.hit()
被调用,因为这就是 Stage.touchDown()
查找位于这些坐标的 Actor
的方式.
关于java - LIBGDX:类扩展 scene2d.Stage 时未调用 hit() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11090711/