0

我已经在 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()");
}
}

提前致谢。

4

2 回答 2

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);
}
于 2013-01-22T19:29:36.757 回答
0

好吧,我会回答我自己的问题。问题是我提供了一个返回 null 的 hit 方法,所以当系统检查时,它从未被击中。

于 2013-01-22T20:45:07.193 回答