无法在Phaser 3游戏中获取音频



我正在学习如何在Phaser3游戏中添加音频。现在,我的代码甚至无法通过预加载音频:

import Phaser from 'phaser'
const gameState = {
playerSize: 50,
playerSpeed: 400,
playerHealth: 3,
playerColor: 0x41E20E,
enemies: []
}
export default class GameScene extends Phaser.Scene
{  

preload()
{
//load sound effects
this.load.audio('hit','/assets/sfx/hitSound.wav')
}
create()
{   //create plater
gameState.player = this.add.rectangle(300, 300, gameState.playerSize, gameState.playerSize, gameState.playerColor); 
this.physics.add.existing(gameState.player)
gameState.player.body.collideWorldBounds = true;

//create cursor keys
gameState.cursor = this.input.keyboard.createCursorKeys();
//create's the first enemy
this.createEnemy();
//Assign sfx
gameState.sfx = {};
gameState.sfx.hit = this.sound.add('hit')
}
update()
{
if (gameState.cursor.left.isDown)
{
gameState.player.body.velocity.x = -1*gameState.playerSpeed
}
else if
(gameState.cursor.right.isDown)
{
gameState.player.body.velocity.x = gameState.playerSpeed
}
else
{ 
gameState.player.body.velocity.x = 0
}
if (gameState.cursor.up.isDown){
gameState.player.body.velocity.y = -1*gameState.playerSpeed
}
else if (gameState.cursor.down.isDown)
{
gameState.player.body.velocity.y = gameState.playerSpeed
}
else
{ 
gameState.player.body.velocity.y = 0
}
}   

createEnemy()
{
let enemy = this.add.circle(300,300,25,0xf00000)    
this.physics.add.existing(enemy)
enemy.body.collideWorldBounds = true
enemy.body.velocity.x = 100
enemy.body.velocity.y = 100
enemy.body.bounce.x = 1
enemy.body.bounce.y = 1
this.physics.add.collider(gameState.player, enemy, ()=>{
gameState.player.body.velocity.x = 0;
gameState.player.body.velocity.x = 0;
enemy.destroy()
})
gameState.enemies.push(enemy)
}
}

我收到错误消息:

phaser.js:123418 Error decoding audio: hit -  Unable to decode audio data
(anonymous) @ phaser.js:123418
phaser.js:106996 Uncaught Error: Audio key "hit" missing from cache
at new WebAudioSound (phaser.js:106996)
at WebAudioSoundManager.add (phaser.js:106577)
at GameScene.create (phaser.js:1)
at SceneManager.create (phaser.js:100720)
at SceneManager.loadComplete (phaser.js:100636)
at LoaderPlugin.emit (phaser.js:2218)
at LoaderPlugin.loadComplete (phaser.js:197141)
at LoaderPlugin.fileProcessComplete (phaser.js:197109)
at AudioFile.onProcessError (phaser.js:5321)
at AudioContext.<anonymous> (phaser.js:123419)
WebAudioSound @ phaser.js:106996
add @ phaser.js:106577
create @ phaser.js:1
create @ phaser.js:100720
loadComplete @ phaser.js:100636
emit @ phaser.js:2218
loadComplete @ phaser.js:197141
fileProcessComplete @ phaser.js:197109
onProcessError @ phaser.js:5321
(anonymous) @ phaser.js:123419
localhost/:1 Uncaught (in promise) DOMException: Unable to decode audio data

我是一个非常新的程序员,所以我不知道我是否缺少了一些愚蠢的东西。我已经三次检查路径和文件名是否正确。所有在线教程都让它看起来很简单。

注意preload,您正在加载/assets/sfx/hitSound.wav。只要移除第一个/就可以了。但这取决于您的bundler配置。

我已经清理了一些你的代码,只留下音频部分。正如你所看到的,它工作正常。

const gameState = {}
class GameScene extends Phaser.Scene
{  
preload()
{
//load sound effects
this.load.audio('working','https://file-examples-com.github.io/uploads/2017/11/file_example_WAV_1MG.wav');
}
create()
{
//Assign sfx
gameState.sfx = {};
gameState.sfx.working = this.sound.add('working');

let playWorkingButton = this.add.text(20, 20, 'PLAY WORKING', { fontSize: 30 });
playWorkingButton.setInteractive();
playWorkingButton.on('pointerdown', () => {
gameState.sfx.working.play();
});
}
}
let game = new Phaser.Game({
scene: GameScene
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.55.2/phaser.min.js"></script>
<div id="game"></div>

相关内容

  • 没有找到相关文章

最新更新