gpt4 book ai didi

java - libgdx 中的声音问题

转载 作者:行者123 更新时间:2023-12-01 11:17:47 25 4
gpt4 key购买 nike

我的按钮的 ClickListener 内的 libgdx 声音存在问题。我收到错误 token “PlayButtonSound”上的语法错误,此 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();
}

}

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

附注我已导入 SoundManager,但由于代码片段的长度而将其保留。

最佳答案

您需要实现 InputListener 之一接口(interface)方法,最有可能 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;
}
});

关于java - libgdx 中的声音问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31590344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com