官网原案例:http://www.phaser.io/examples/v2/audio/audio-sprite-duration
效果图:
注意:如果播放了多次soundscape你会发现暂停不了多个,只会暂停一个
这边我我做了如下修改
//为了只同时播放一段背景音乐
if(button.name == "soundscape" && (fx.isPlaying || fx.paused))
return;
另外在用game.debug.soundInfo
显示了调试信息
代码:
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create ,render:render});
function preload() {
game.load.image('bg', '../assets/pics/cougar_dragonsun.png');
game.load.spritesheet('button', '../assets/buttons/flixel-button.png', 80, 20);
game.load.bitmapFont('nokia', '../assets/fonts/bitmapFonts/nokia16black.png', '../assets/fonts/bitmapFonts/nokia16black.xml');
game.load.audio('sfx', [ '../assets/audio/SoundEffects/magical_horror_audiosprite.mp3', '../assets/audio/SoundEffects/magical_horror_audiosprite.ogg' ]);
}
//audio对象
var fx;
function create() {
var bg = game.add.image(0, 0, 'bg');
bg.width = 800;
bg.height = 600;
fx = game.add.audio('sfx');
//是否允许您一次播放此声音的多个实例
fx.allowMultiple = false;
fx.addMarker('charm', 0, 2.7);
fx.addMarker('curse', 4, 2.9);
fx.addMarker('fireball', 8, 5.2);
fx.addMarker('spell', 14, 4.7);
fx.addMarker('soundscape', 20, 18.8);
makeButton('charm', 600, 100);
makeButton('curse', 600, 140);
makeButton('fireball', 600, 180);
makeButton('spell', 600, 220);
makeButton('soundscape', 600, 260);
makeButton('pause', 600, 380);
}
function makeButton(name, x, y) {
var button = game.add.button(x, y, 'button', click, this, 0, 1, 2);
button.name = name;
button.scale.set(2, 1.5);
button.smoothed = false;
var text = game.add.bitmapText(x, y + 7, 'nokia', name, 16);
text.x += (button.width / 2) - (text.textWidth / 2);
}
function click(button) {
//暂停继续音乐
if (button.name === 'pause')
{
if (fx.paused)
fx.resume();//继续
else
fx.pause();//暂停
}
else{
//为了只同时播放一段背景音乐
if(button.name == "soundscape" && (fx.isPlaying || fx.paused))
return;
fx.play(button.name);
}
}
function render(){
game.debug.soundInfo(fx,100,100);
}