libgdx 中的声音问题

Issue with sounds in libgdx

我的按钮 ClickListener 中的 libgdx 声音有问题。 我收到错误 Syntax error on token "PlayButtonSound", Identifier expected after this token

声音管理器:

import utils.Constants;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;

public class SoundManager {

    private static Sound buttonSound = Gdx.audio.newSound(Constants.buttonSound);
    private static Music song = Gdx.audio.newMusic(Constants.song);

    public static void PlayMusic() {
        song.setLooping(true);
        song.setVolume(0.2f);
        song.play();
    }

    public static void PlayButtonSound() {
        buttonSound.play();
    }

    public void DestryoAudio() {
        buttonSound.dispose();
        song.dispose();

    }
}

和主菜单:

public class MainMenu implements Screen {

    private Stage stage;
    private Sprite sprite;
    private TextureRegion menuBackgroundImg;
    private TextureAtlas menuButton;
    private Skin skin;
    private BitmapFont font;
    private Table table;
    private TextButton playButton;

    @Override
    public void show() {
        // Set stage
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        SoundManager.PlayMusic();

        // Set menu baggrund
        menuBackgroundImg = new TextureRegion(new Texture(Gdx.files.internal(Constants.menuBackgroundImg)));
        sprite = new Sprite(menuBackgroundImg);
        sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        // Set menuknapper      
        menuButton = new TextureAtlas(Constants.menuButton);
        skin = new Skin(menuButton);

        // Set font
        font = new BitmapFont(Constants.font, false);

        // Set table
        table = new Table(skin);
        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        // Create button styling
        TextButtonStyle textButtonStyle = new TextButtonStyle();
        textButtonStyle.up = skin.getDrawable("menuButtonUp");
        textButtonStyle.down = skin.getDrawable("menuButtonPressed");
        textButtonStyle.pressedOffsetX = 1;
        textButtonStyle.pressedOffsetY = -1;
        textButtonStyle.font = font;
        textButtonStyle.fontColor = Color.BLACK;

        // Create buttons
        playButton = new TextButton(Constants.play, textButtonStyle);
        playButton.addListener(new InputListener() {
            SoundManager.PlayButtonSound(); // This is the error
        });

        // Button padding
        playButton.pad(20, 100, 20, 100);

        // Setting the table
        table.add(playButton).row();

        // Setting stage actor
        stage.addActor(table);      
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Tegn baggrund
        stage.getBatch().begin();
        sprite.draw(stage.getBatch());
        stage.getBatch().end();

        // Tegn resten
        stage.act(delta);
        stage.draw();

    }

...

    @Override
    public void dispose() {
        stage.dispose();
        menuButton.dispose();
        skin.dispose();
        font.dispose();
    }

}

我无法真正理解我做错了什么,搜索错误给出了模糊的答案,并没有真正解决我的问题。

P.S。我已导入 SoundManager 但由于代码片段的长度而将其遗漏。

您需要实现 InputListener 接口方法之一,很可能是 touchDown(InputEvent event, float x, float y, int pointer, int button)

查看 API for InputListener。它列出了所有方法并给出了一个很好的例子。

playButton.addListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        SoundManager.PlayButtonSound();
        return false;
    }
});