如何让按钮在Libgdx中播放声音?

时间:2015-01-20 20:23:58

标签: java libgdx

当鼠标悬停在TextButton上时,如何让TextButton播放声音?类似的东西:

TextButton play = new TextButton("Play", textButtonStyle);
play.addListener(new ButtonHoverListener() {
    @Override
    public void doSomething() {
        ...
    }
});

1 个答案:

答案 0 :(得分:3)

play.addListener(new InputListener(){

        boolean playing = false;

        @Override
        public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            super.enter(event, x, y, pointer, fromActor);
            if (!playing) {
                Sound sound = Gdx.audio.newSound(Gdx.files.internal("data/mysound.mp3"));
                sound.play(1F);
                playing = true;
            }
        }

        @Override
        public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
            super.exit(event, x, y, pointer, toActor);
            playing = false;
        }
    });