2013-01-22 51 views
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()"); 
} 
} 

在此先感謝。

回答

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); 
} 
+0

嗨,我已經完成了你所做的,我現在正在接收My Screen類的輸入,但我的精靈尚未接收輸入:/。另一方面,我可以理解你做了什麼,但是將場景設置爲輸入處理器不會一樣嗎?你真的這樣做。只要你在屏幕上獲得輸入,然後委託給舞臺。順便說一句,非常感謝你在這裏舉手。 – Notbad

0

好,我會回答我自己的問題。問題在於我提供了一個返回null的命中方法,因此在通過系統檢查時這從來沒有被打過。