I’m using bevy 0.14.1 to create a small game and I have some problems with the audio. In my game, there is a character that takes coin. Each time an user takes a coin, a sound must be played. I managed that the audio was played when the character took the first coin, but i couldn’t make the audio be replayed again when the user takes another coin. Here’s my code: What i did was an AudioBundle and i associated it to my entity(score):
impl Score {
fn new() -> Self {
Self(0)
}
}
let score_text = Text2dBundle {
text: Text::from_section("0", text_style).with_justify(JustifyText::Left),
transform: Transform::from_translation(POSITION_SCORE),
..default()
};
let audio_score = AudioBundle {
source: audio_asset.score.clone(),
settings: PlaybackSettings {
paused: true,
mode: PlaybackMode::Once,
..default()
},
};
command.spawn((score_text, Score::new(), audio_score)); `
then i made a system to change the score and play the audio when it happens:
fn change_score(mut query: Query<(&mut Text, &mut Score, &AudioSink)>) {
for(mut text, mut score_component, audio_sink) in &mut query {
if let Some(point) = text.sections.first_mut() {
score_component.0 += 1;
point.value = score_component.0.to_string();
audio_sink.play();
}
}
}
I realised that after audio_sink.play() is run and the sound is played, the sink is empty, i mean, the sink has no more audio to play.On the other hand, I know that change_score system is called as the number of point(score_component.0) is changed when an user takes a coin but the sound isn’t replayed again.
Thank you.
Javier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1