我已经在stackoverflow上阅读了一些答案,试图弄清楚为什么我的SpriteActor输入事件没有运气而没有火。不知道我做错了什么。如果有人可以提供帮助,我会非常感激。
- 屏幕类
public class MainGameScreen implements Screen
{
Stage _stage;
public MainGameScreen()
{
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
_stage=new Stage(w,h,true);
Gdx.input.setInputProcessor(_stage);
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texturesHD.atlas"));
SpriteActor actor= new SpriteActor(atlas,"BtTemple",true);
actor.x=w/2.0f;
actor.y=h/2.0f;
_stage.addActor(actor);
}
public void dispose()
{
}
public void hide()
{
}
public void resize(int width, int height)
{
}
public void resume()
{
}
public void show()
{
}
public void render(float deltaTime)
{
_stage.draw();
}
public void pauseGame()
{
}
// this is called by android
public void pause()
{
}
}
这是我的SpriteActor类:
class SpriteActor extends Actor
{
private Sprite _sprite;
public SpriteActor(TextureAtlas atlas, String regionName, boolean touchable)
{
super();
_sprite = atlas.createSprite(regionName);
//setWidth(_sprite.getWidth());
//setHeight(_sprite.getHeight());
//setBounds(x,y,getWidth(),getHeight());
this.touchable=touchable;
}
@Override
public void draw(SpriteBatch batch, float parentAlpha)
{
/*Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);*/
batch.draw(_sprite, x, y);
}
@Override
public Actor hit(float x, float y)
{
return null;
}
@Override
public boolean touchDown (float x, float y, int pointer)
{
Gdx.app.debug("Game", "TestActor.touchDown()");
return true; // must return true for touchUp event to occur
}
@Override
public void touchUp (float x, float y, int pointer)
{
Gdx.app.debug("Game", "TestActor.touchUp()");
}
}
提前致谢。
答案 0 :(得分:0)
尝试这样的事情:
public class MainGameScreen implements Screen, InputProcessor {
将setInputProcessor移动到show()方法,如下所示:
@Override
public void show() {
Gdx.input.setInputProcessor(this);
}
并实现InputProcessor中的方法:
@Override
public boolean keyDown(int keycode) {
//do whatever you want
return _stage.keyDown(keycode);
}
答案 1 :(得分:0)
好吧,我会回答我自己的问题。问题是我提供了一个返回null的命中方法,所以当系统检查时,它从未被命中。