我正在try 将点击按钮时播放的声音随机化,但我也想跳过最后播放的声音.例如,如果Sfx4
将连续播放四次,这听起来就不自然了.我想总是播放一种新的声音(当然是在这5种声音中).我做错了什么?如果可能的话,给我一些关于如何实施它的 idea .
<template>
<button class="book__nav-btn" @click="playTurnSfx">
<slot> </slot>
</button>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue';
import Sfx1 from '@/assets/sounds/page-turn-1.mp3?url';
import Sfx2 from '@/assets/sounds/page-turn-2.mp3?url';
import Sfx3 from '@/assets/sounds/page-turn-3.mp3?url';
import Sfx4 from '@/assets/sounds/page-turn-4.mp3?url';
import Sfx5 from '@/assets/sounds/page-turn-5.mp3?url';
const sounds = [Sfx1, Sfx2, Sfx3, Sfx4, Sfx5];
const lastPlayedIndex = ref(null);
let playableSounds = sounds.toSpliced(lastPlayedIndex.value, 1);
const randomIndex = computed(() => {
return Math.floor(Math.random() * playableSounds.length);
});
const playTurnSfx = () => {
const sound = new Audio(sounds[randomIndex.value]);
sound.volume = 0.1;
sound.play();
};
</script>