在 Phaser3 class 方法之间共享变量的最佳方式是什么?
What is the best way to share variables across Phaser3 class methods?
我正在尝试找到一种干净、可读的方式来管理 Phaser 中跨函数的变量 Class,但出于各种原因,我对找到的解决方案不满意。
据我所知可用:
全局变量
我不太喜欢这个实现,因为其他文件可能会访问变量。
var heroes = [];
var play = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function(){
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
for(var i = 0; i < 5; i++){
heroes.add(new Hero())
}
},
update: function(){
if(!heroes.length){
heroes.add(new Hero())
}
heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
}
});
DataManager class(作为注册表实现)
我更喜欢这个,因为它更受控制,但对我来说,DataManager 感觉它是为配置而设计的,而不是作为 process/share 在 class 方法之间获取数据的手段;对于获取和设置其值的特定服务,访问和更新变量也感觉非常繁琐。
var play = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function(){
this.registry.set('heroes', []);
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
var heroes = this.registry.get('heroes');
for(var i = 0; i < 5; i++){
heroes.add(new Hero())
}
this.registry.set('heroes', heroes);
},
update: function(){
var heroes = this.registry.get('heroes');
if(!heroes.length){
heroes.add(new Hero())
}
heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
this.registry.set('heroes', heroes);
}
});
使用'this'
到目前为止,这对我来说是最干净的方式,因为它指的是 class 对象,并且很容易更新和检索值,但在此上下文中 'this' 与某些内部 Phaser 特定共享我想从中分离变量的变量。除了命名空间,还有其他解决方案吗?
var play = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function(){
this.heroes = [];
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
for(var i = 0; i < 5; i++){
this.heroes.add(new Hero())
}
},
update: function(){
if(!heroes.length){
this.heroes.add(new Hero())
}
this.heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
}
});
在我看来,最干净、最易读的方法是使用适当的 classes(而不是 Phaser.Class
的实例)。您可以随时扩展所需的 Phaser class(如此处的 Phaser.Scene
)。
打字稿:
class Play extends Phaser.Scene {
private heroes: Hero[] = [];
private create(): void {
for (let i = 0; i < 5; i++) {
this.heroes.push(new Hero());
}
}
private update(): void {
if (!this.heroes.length) {
this.heroes.push(new Hero());
}
this.heroes.forEach((hero) => {
if (hero.hp <= 0) {
hero.destroy();
}
});
}
public initialize(): void {
Phaser.Scene.call(this, { key: 'play' });
}
}
ES6(相同,除了类型声明和访问修饰符):
class Play extends Phaser.Scene {
heroes = [];
create() {
for (let i = 0; i < 5; i++) {
this.heroes.push(new Hero());
}
}
update() {
if (!this.heroes.length) {
this.heroes.push(new Hero());
}
this.heroes.forEach((hero) => {
if (hero.hp <= 0) {
hero.destroy();
}
});
}
initialize() {
Phaser.Scene.call(this, { key: 'play' });
}
}
如果您出于某种原因坚持使用 ES5,那么您最后的建议可能是您最好的选择。
刚刚意识到另一种方法是使用自调用函数:
在这个例子中,英雄将在函数范围内,也不应该污染 return 对象;
var play = new Phaser.Class(function(){
var heroes = [];
return {
Extends: Phaser.Scene,
initialize: function(){
heroes = [];
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
for(var i = 0; i < 5; i++){
heroes.add(new Hero())
}
},
update: function(){
if(!heroes.length){
heroes.add(new Hero())
}
heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
}
}}());
尝试在场景之间传递一些全局数据,发现如下。每个场景都有自己的全局注册表引用。这是文档中的引述:
This is a game-wide instance of the Data Manager, allowing you to exchange data between Scenes via a universal and shared point.
In the default set-up you can access this from within a Scene via the this.registry
property.
https://photonstorm.github.io/phaser3-docs/Phaser.Data.DataManager.html
这是包含 class 详细描述的文档。最近在玩游戏试了一下,用起来很方便
我正在尝试找到一种干净、可读的方式来管理 Phaser 中跨函数的变量 Class,但出于各种原因,我对找到的解决方案不满意。
据我所知可用:
全局变量
我不太喜欢这个实现,因为其他文件可能会访问变量。
var heroes = [];
var play = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function(){
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
for(var i = 0; i < 5; i++){
heroes.add(new Hero())
}
},
update: function(){
if(!heroes.length){
heroes.add(new Hero())
}
heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
}
});
DataManager class(作为注册表实现)
我更喜欢这个,因为它更受控制,但对我来说,DataManager 感觉它是为配置而设计的,而不是作为 process/share 在 class 方法之间获取数据的手段;对于获取和设置其值的特定服务,访问和更新变量也感觉非常繁琐。
var play = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function(){
this.registry.set('heroes', []);
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
var heroes = this.registry.get('heroes');
for(var i = 0; i < 5; i++){
heroes.add(new Hero())
}
this.registry.set('heroes', heroes);
},
update: function(){
var heroes = this.registry.get('heroes');
if(!heroes.length){
heroes.add(new Hero())
}
heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
this.registry.set('heroes', heroes);
}
});
使用'this'
到目前为止,这对我来说是最干净的方式,因为它指的是 class 对象,并且很容易更新和检索值,但在此上下文中 'this' 与某些内部 Phaser 特定共享我想从中分离变量的变量。除了命名空间,还有其他解决方案吗?
var play = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function(){
this.heroes = [];
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
for(var i = 0; i < 5; i++){
this.heroes.add(new Hero())
}
},
update: function(){
if(!heroes.length){
this.heroes.add(new Hero())
}
this.heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
}
});
在我看来,最干净、最易读的方法是使用适当的 classes(而不是 Phaser.Class
的实例)。您可以随时扩展所需的 Phaser class(如此处的 Phaser.Scene
)。
打字稿:
class Play extends Phaser.Scene {
private heroes: Hero[] = [];
private create(): void {
for (let i = 0; i < 5; i++) {
this.heroes.push(new Hero());
}
}
private update(): void {
if (!this.heroes.length) {
this.heroes.push(new Hero());
}
this.heroes.forEach((hero) => {
if (hero.hp <= 0) {
hero.destroy();
}
});
}
public initialize(): void {
Phaser.Scene.call(this, { key: 'play' });
}
}
ES6(相同,除了类型声明和访问修饰符):
class Play extends Phaser.Scene {
heroes = [];
create() {
for (let i = 0; i < 5; i++) {
this.heroes.push(new Hero());
}
}
update() {
if (!this.heroes.length) {
this.heroes.push(new Hero());
}
this.heroes.forEach((hero) => {
if (hero.hp <= 0) {
hero.destroy();
}
});
}
initialize() {
Phaser.Scene.call(this, { key: 'play' });
}
}
如果您出于某种原因坚持使用 ES5,那么您最后的建议可能是您最好的选择。
刚刚意识到另一种方法是使用自调用函数:
在这个例子中,英雄将在函数范围内,也不应该污染 return 对象;
var play = new Phaser.Class(function(){
var heroes = [];
return {
Extends: Phaser.Scene,
initialize: function(){
heroes = [];
Phaser.Scene.call(this, {key: 'play'});
},
create: function () {
for(var i = 0; i < 5; i++){
heroes.add(new Hero())
}
},
update: function(){
if(!heroes.length){
heroes.add(new Hero())
}
heroes.forEach(function(hero){
if(hero.hp <= 0){
hero.destroy();
}
});
}
}}());
尝试在场景之间传递一些全局数据,发现如下。每个场景都有自己的全局注册表引用。这是文档中的引述:
This is a game-wide instance of the Data Manager, allowing you to exchange data between Scenes via a universal and shared point.
In the default set-up you can access this from within a Scene via thethis.registry
property.
https://photonstorm.github.io/phaser3-docs/Phaser.Data.DataManager.html 这是包含 class 详细描述的文档。最近在玩游戏试了一下,用起来很方便