我只想在舞台上的 Actor 被触摸时执行一个功能.. 但问题是,无论函数触及舞台上的哪个位置,函数都会被执行。 即使它触摸了舞台上的某个随机位置,该功能也会被执行。 我希望该函数仅在 Actor 被触摸时执行..我已经设置了边界...但它仍然不起作用..
public Restart()
{
atlas = new TextureAtlas(Gdx.files.internal("pages-info.atlas"));
sprite = atlas.createSprite("restart");
this.touchable = true;
sprite.setBounds(x, y, sprite.getWidth(), sprite.getHeight());
}
public void draw(SpriteBatch batch,float parentAlpha)
{
batch.draw(sprite, x, y , width, height );
}
@Override
public Actor hit(float x, float y)
{
// TODO Auto-generated method stub
Gdx.app.log( FirstGame.LOG, " restart working " );
return null;
}
最佳答案
hit
方法没有按照您想象的那样执行。 hit
方法用于测试当前 actor 是否与给定的 x,y 相交,因此始终会调用它。 (如果您因为 Actor 不是矩形而想丢弃“命中”,那么它很有用。)
使用addListener
添加事件监听器来接收触摸事件并对它们使用react。
关于java - 触摸事件无法正常工作..libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592303/