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

3
当鼠标悬停在TextButton上时,如何使其播放声音?例如:
TextButton play = new TextButton("Play", textButtonStyle);
play.addListener(new ButtonHoverListener() {
    @Override
    public void doSomething() {
        ...
    }
});

2
请查看此问题:https://dev59.com/s37aa4cB1Zd3GeqPv-Kl - Alexander Mironov
@AlexanderMironov 那很有道理。谢谢你。 - name
1个回答

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

哦,谢谢。我实际上阅读了亚历山大·米罗诺夫发布的链接,ClickListener 也有这些方法。 - name
InputListener比ClickListener更通用。 - WeMakeSoftware

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接