Evilunclebill
Evilunclebill

Reputation: 789

Issue with sounds in libgdx

I am having issues with libgdx sound inside a ClickListener for my button. I am getting the error Syntax error on token "PlayButtonSound", Identifier expected after this token

SoundManager:

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

    }
}

And MainMenu:

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

}

I can't really grasp what I am doing wrong and a search for the error gives vague answers that dosen't really solves my issue.

P.S. I have imported SoundManager but left it out due to length of the code snippet.

Upvotes: 0

Views: 129

Answers (1)

Ryan Jackman
Ryan Jackman

Reputation: 780

You need to implement one of the InputListener interface methods, most likely touchDown(InputEvent event, float x, float y, int pointer, int button).

Check out the API for InputListener. It lists all the methods and gives a pretty good example.

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

Upvotes: 1

Related Questions