Recommended Posts

Hey

I am currently polishing off my Breakout or Brick-Breaker game (whichever you prefer to call it). I am trying to add music to the game when the start button is pressed and then sound effects every time a brick is destroyed.

Unfortunately Phaser keeps saying Key "Sample" is not found in cache.

I have tried using Phasers examples and basing the music off that but to no avail.

I am new to programming with Phaser and would really appreciate the help. When the game is finished i will post a link.

Thanks in Advance

Pheaset

here is my code

var game = new Phaser.Game(480, 320, Phaser.AUTO, null, {
    preload: preload, create: create, update: update
});
var firework;;

var music = Phaser.Sound;

var lives = 1;
var livesText;
var lifeLostText;
var ball;
var paddle;
var bricks;
var newBrick;
var brickInfo;
var scoreText;
var score = 0;
var playing = false;
var startButton;
function preload() {
    game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    game.scale.pageAlignHorizontally = true;
    game.scale.pageAlignVertically = true;
    game.stage.backgroundColor = '#eee';
    game.load.image('ball',  'ball.png');
    game.load.image('paddle','paddle.png');
    game.load.image('brick', 'img/brick.png')
    game.load.spritesheet('ball', 'img/wobble.png', 20, 20);
    game.load.spritesheet('button','button.png', 120, 40);
    game.load.audio('Good-Riddance', 'Good-Riddance.mp3');
}
function create() {
      game.physics.startSystem(Phaser.Physics.ARCADE);
      startButton = game.add.button(game.world.width*0.5, game.world.height*0.5, 'button', startGame, this, 1, 0, 2);
startButton.anchor.set(0.5);

      music = game.add.audio('Good-Riddance')
      this.music = this.add.audio("Good-Riddance")
      this.game.sound.setDecodedCallback(["Good-Riddance"]);
      if(playing === true)
      {
            music.play()
      }else{
        music.mute = false;
      }

 

Share this post


Link to post
Share on other sites

I start my music samples like this:

// that bit is important for RAM consumption you usualy dont need WebAudio if you dont do fancy things
window.PhaserGlobal = {
  disableWebAudio: true
};
var game = new Phaser.Game(480, 320, Phaser.AUTO, null, {
  preload: function() { 
    game.load.audio('Good-Riddance', 'Good-Riddance.mp3');
  },
  create: function() {
    var soundtrack = game.sound.play(
      'Good-Riddance',//cache key
      1,//volume
      true//loop
    );
    soundtrack.mute = false;// if you want to mute it
  }
})

If something doesnt work check browser console for errors F12 maybe your file is in different place?

Or you running script from file path instead of server can be problem sometimes but should be in this case but if it is you can use for example brackets editor that starts small development server for serving your files.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.