libgdx中的声音问题

时间:2015-07-23 14:29:44

标签: java libgdx

我的按钮PolicyID MemberType MemberAddress MemberCity -------------------------------------------------- 1234 Owner 9785 sw 197 ct Miami 1234 Dependent NULL NULL 1234 Spouse NULL NULL 内有libgdx声音问题。 我收到错误PolicyID MemberType MemberAddress MemberCity ---------------------------------------------------- 1234 Owner 9785 sw 197 ct Miami 1234 Dependent 9785 sw 197 ct Miami 1234 Spouse 9785 sw 197 ct Miami

SoundManager类:

ClickListener

MainMenu:

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

    }
}

我无法真正理解我做错了什么,并且对错误的搜索会给出模糊的答案,这些答案实际上解决了我的问题。

P.S。我已导入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(); } ,但由于代码段的长度而将其遗漏。

1 个答案:

答案 0 :(得分:1)

您需要实施一种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;
    }
});