Penniwhistle Posted September 17, 2014 Share Posted September 17, 2014 Hey there - my apologies for what's probably a dumb question - totally new to JS and Phaser, and I couldn't find anything on the web to answer this. Followed a tutorial to make a game, and am now trying to add to it. Have a background track, set to repeat. When the character dies, the game resets instantly. However the music continues AND another track of it starts. Sounds horrendous. How do I kill the track on game reset, so after a death the background music restarts? Using version 2.0.5 if it's relevant - next project I do will be with the most up-to-date. Code here:var game = new Phaser.Game(400, 490, Phaser.AUTO, 'gameDiv');var mainState = { preload: function() { game.stage.backgroundColor = '#71c5cf'; game.load.image('bird', 'assets/bird.png'); game.load.image('pipe', 'assets/pipe.png');// Firefox doesn't support mp3 files, so use ogg game.load.audio('bgMusic', ['assets/bg.mp3', 'assets/bg.ogg']); // Load the jump sound game.load.audio('jump', 'assets/jump.wav'); }, create: function() { game.physics.startSystem(Phaser.Physics.ARCADE); this.pipes = game.add.group(); this.pipes.enableBody = true; this.pipes.createMultiple(20, 'pipe'); this.timer = this.game.time.events.loop(1500, this.addRowOfPipes, this); this.bird = this.game.add.sprite(100, 245, 'bird'); game.physics.arcade.enable(this.bird); this.bird.body.gravity.y = 1000; // New anchor position this.bird.anchor.setTo(-0.2, 0.5); var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); spaceKey.onDown.add(this.jump, this); this.score = 0; this.labelScore = this.game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" }); // Add the jump sound this.jumpSound = this.game.add.audio('jump'); // Game music var music; music = game.add.audio('bgMusic',1,true); music.play('',0,1,true); }, update: function() { if (this.bird.inWorld == false) this.restartGame(); game.physics.arcade.overlap(this.bird, this.pipes, this.hitPipe, null, this); // Slowly rotate the bird downward, up to a certain point. if (this.bird.angle < 20) this.bird.angle += 1; }, jump: function() { // If the bird is dead, he can't jump if (this.bird.alive == false) return; this.bird.body.velocity.y = -350; // Jump animation game.add.tween(this.bird).to({angle: -20}, 100).start(); // Play sound this.jumpSound.play(); }, hitPipe: function() { // If the bird has already hit a pipe, we have nothing to do if (this.bird.alive == false) return; // Set the alive property of the bird to false this.bird.alive = false; // Prevent new pipes from appearing this.game.time.events.remove(this.timer); // Go through all the pipes, and stop their movement this.pipes.forEachAlive(function(p){ p.body.velocity.x = 0; }, this); }, restartGame: function() { game.state.start('main'); }, addOnePipe: function(x, y) { var pipe = this.pipes.getFirstDead(); pipe.reset(x, y); pipe.body.velocity.x = -200; pipe.checkWorldBounds = true; pipe.outOfBoundsKill = true; }, addRowOfPipes: function() { var hole = Math.floor(Math.random()*5)+1; for (var i = 0; i < 8; i++) if (i != hole && i != hole +1) this.addOnePipe(400, i*60+10); this.score += 1; this.labelScore.text = this.score; }};game.state.add('main', mainState);game.state.start('main'); Link to comment Share on other sites More sharing options...
lewster32 Posted September 17, 2014 Share Posted September 17, 2014 Just stop the music playing in your restartGame function, by keeping a reference on the state instead of locally:create: function() { this.music = game.add.audio('bgMusic', 1, true);}restartGame: function() { this.music.stop();}In a game I recently made, I instead added the music to a global variable which checked to see if it had been defined and had a playing audio instance - if it was, it did nothing, if it wasn't, it set it - something like this:if (!GlobalGame.music || !GlobalGame.music.isPlaying) { GlobalGame.music = this.game.add.audio('music', 1, true);}This allowed the music to start at the beginning of the game, and continue uninterrupted whenever the game reset or changed states. BrunoHautenfaust and juandelossantos 2 Link to comment Share on other sites More sharing options...
Penniwhistle Posted September 17, 2014 Author Share Posted September 17, 2014 Worked perfectly, thank you. The music is literally about 4 seconds long repeating - hardly a masterpiece. But the global entry to keep playing music will be handy later on if I keep at this, and have something better to play. Link to comment Share on other sites More sharing options...
Recommended Posts