libgdx actor输入不起作用

时间:2017-01-17 14:52:18

标签: java libgdx actor stage

我的libgdx中的actor通常会绘制inputlistener无法正常工作,我已经检查过我启动了Gdx.input.setInputProcessor并且我非常确定我的界限设置正确所以请帮我解决这个问题

代码:

Sprite playButton = new Sprite(new Texture(Gdx.files.internal("badlogic.jpg")));


public playBtn(){
    setBounds(playButton.getX(),playButton.getY(),getWidth(),getHeight());
    setTouchable(Touchable.enabled);
    addListener(new InputListener(){
        @Override
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.log("Example", "touch started at (" + x + ", " + y + ")");
            return false;
        }
        @Override
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.log("Example", "touch done at (" + x + ", " + y + ")");
        }

    });



}

@Override
public void draw(Batch batch, float parentAlpha) {
    playButton.draw(batch);


}

@Override
public void act(float delta) {
    super.act(delta);
}

主要课程:

    public MenuState(GameStateManager gsm){
    super(gsm);


    ScreenViewport screenViewport = new ScreenViewport();
    menuStage = new Stage(screenViewport);
    playBtn playButton= new playBtn();
    menuStage.addActor(playButton);
    Gdx.input.setInputProcessor(menuStage);



    background = new Sprite(new Texture("MenuBG.png"));
    project = new Project();
    background.setSize(project.WIDTH,project.HEIGHT);


}


@Override
protected void handleInput() {

}

@Override
public void update(float dt) {
    handleInput();

}



@Override
public void render(SpriteBatch sb) {



    sb.begin();
    background.draw(sb);
    sb.end();
    menuStage.act(Gdx.graphics.getDeltaTime());
    menuStage.draw();

}

1 个答案:

答案 0 :(得分:0)

尝试:

setBounds(playButton.getX(), playButton.getY(), playButton.getWidth(), playButton.getHeight())