问题描述
我的问题是关于的特定问题 Phaser游戏引擎
My question is specific about Phaser game engine
所以我有一个带有几个状态的游戏,每个状态的定义如下:
So I have a game with a few states and each state is defined like:
var myGame = {};
myGame.Boot = function (game) {
};
myGame.Boot.prototype = {
init: function () {
//some init
},
create: function () {
//this.scale.scaleMode
this.state.start('Preloader');
}
};
游戏定义如下:
var game = new Phaser.Game(gameWidth, gameHeight, Phaser.AUTO, 'main');
game.state.add('Boot', myGame.Boot);
game.state.add('Preloader', myGame.Preloader);
game.state.add('MainMenu', myGame.MainMenu);
正常,简单,标准.一切都很好(到目前为止).
Normal, simple, standard. All good (so far).
这是我在预加载器"中定义音乐的方式(与这里):
Here is how I'm defining a music in my 'Preloader' (same as here):
fx = game.add.audio('sfx');
fx.allowMultiple = true;
fx.addMarker('alien death', 1, 1.0);
fx.addMarker('boss hit', 3, 0.5);
fx.addMarker('escape', 4, 3.2);
fx.addMarker('meow', 8, 0.5);
fx.addMarker('numkey', 9, 0.1);
this.sound.setDecodedCallback(
[gg.fx],
this.start, this
);
其中fx是全局变量.然后,如果我需要播放音乐,我会
where fx is a global variable. And then if I need to play a music I do
fx.play(button.name);
代码有效,但是我必须始终保留此全局变量(或将其传递给需要音乐的每个州).
The code works, but I have to always keep this global var (or pass it to every state where I need a music).
- 我不喜欢全局变量.有更好的方法吗?我打算有很多效果,我想有一种简单的方法来管理它们.
- 如果我不使用全局var并调用类似this.sound.play("name")的代码,则表示该文件不在缓存中.有没有一种方法可以将其添加到全局缓存中?这是个坏主意吗?
谢谢!
推荐答案
- 全局变量:
您实际上可以在myGame
上定义它们.虽然它使用不同的方式来定义它们,但请查看全屏移动模板,它是Phaser代码库本身的一部分. Phaser的创建者实际上在其论坛上建议使用此变量来声明全局"变量.
You could actually define these on myGame
. While it uses a different way of defining these, take a look at the Full Screen Mobile Template that's part of the Phaser codebase itself. The creator of Phaser actually recommended this on their forums for declaring 'global' variables.
这给你这样的东西:
myGame.fx = game.add.audio('sfx');
// ...
- 全局缓存:
好问题.我发现的唯一一件事就是分解了MP3文件,并将它们分别加载到缓存中.这样,您的缓存中就有一个具有该名称/键的对象.
Good question. The only thing I've found is breaking the MP3 files up and loading them into the cache individually. In that way your cache has an object with that name/key.
但是,如果将它们添加到myGame
对象中,则应该可以将音频保存在单个文件中.
But if you add these to your myGame
object, you should be able to keep your audio in a single file.
这篇关于在Phaser游戏引擎的状态之间传递音乐对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!