Phaser.js |在更改状态之前删除 tilemap
Phaser.js | Remove tilemap before change state
我有一个多游戏项目。我有一个 menu.js
和很多游戏 js 页面。
在menu.js
加载页面,视图是正确的。在 game.js
和 menu.js
中加载 return 后,屏幕不一样了。我尝试了很多解决方案,但没有任何效果。我想一开始就显示相同的外观。
Menu.js
加载前 game.js
Menu.js
加载后 game.js
menu.js
的一部分
var menuState = {
create: function(){
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.setGameSize(1000, 600);
game.scale.minWidth = 300;
game.scale.minHeight = 180;
game.scale.maxWidth = 1000;
game.scale.maxHeight = 600;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.scale.updateLayout(true);
game.state.start("game");
},
start: function(){
}
}
game.js
的一部分
this.map = game.add.tilemap('level2');
this.map.addTilesetImage('secondMap');
this.map.setCollisionByExclusion([13, 14, 15, 16, 46, 47, 48, 49, 50, 51]);
this.map.setTileIndexCallback(225, this.test3, this);
this.map.setTileIndexCallback(228, this.test, this);
this.map.setTileIndexCallback(231, this.test2, this);
this.layer = this.map.createLayer('Tile Layer 1');
this.layer.resizeWorld();
如果我将 this.layer.resizeWorld();
和 return 删除到主页,外观是正确的,而且我认为是 map
使 return 外观变得糟糕。
我试过:
this.layer.destroy();
this.map.destroy();
game.world.removeAll()
game.state.clearCurrentState();
但结果总是一样。
如果我使用:
game.state.destroy();
game.destroy();
全部删除。
我看不出问题出在哪里。
我找到了解决方案。 return后的世界width
和height
需要重新定义到主菜单。也有必要为您要加载的所有游戏定义世界 width
和 height
。
就我而言:
var menuState = {
create: function(){
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.setGameSize(1000, 600);
game.scale.minWidth = 300;
game.scale.minHeight = 180;
game.scale.maxWidth = 1000;
game.scale.maxHeight = 600;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
//------------------------
game.world.width = 1000;
game.world.height = 600;
//------------------------
game.scale.updateLayout(true);
game.state.start("game");
},
start: function(){
}
}
我也遇到了类似的问题,但是我在 Google 中找到了一个解决方案,它对我有用(在我的情况下)并且会认真地重新定义世界的极限:
this.game.world.setBounds(0, 0, this.game.width, this.game.height);
我有一个多游戏项目。我有一个 menu.js
和很多游戏 js 页面。
在menu.js
加载页面,视图是正确的。在 game.js
和 menu.js
中加载 return 后,屏幕不一样了。我尝试了很多解决方案,但没有任何效果。我想一开始就显示相同的外观。
Menu.js
加载前 game.js
Menu.js
加载后 game.js
menu.js
的一部分var menuState = {
create: function(){
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.setGameSize(1000, 600);
game.scale.minWidth = 300;
game.scale.minHeight = 180;
game.scale.maxWidth = 1000;
game.scale.maxHeight = 600;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.scale.updateLayout(true);
game.state.start("game");
},
start: function(){
}
}
game.js
的一部分this.map = game.add.tilemap('level2');
this.map.addTilesetImage('secondMap');
this.map.setCollisionByExclusion([13, 14, 15, 16, 46, 47, 48, 49, 50, 51]);
this.map.setTileIndexCallback(225, this.test3, this);
this.map.setTileIndexCallback(228, this.test, this);
this.map.setTileIndexCallback(231, this.test2, this);
this.layer = this.map.createLayer('Tile Layer 1');
this.layer.resizeWorld();
如果我将 this.layer.resizeWorld();
和 return 删除到主页,外观是正确的,而且我认为是 map
使 return 外观变得糟糕。
我试过:
this.layer.destroy();
this.map.destroy();
game.world.removeAll()
game.state.clearCurrentState();
但结果总是一样。
如果我使用:
game.state.destroy();
game.destroy();
全部删除。
我看不出问题出在哪里。
我找到了解决方案。 return后的世界width
和height
需要重新定义到主菜单。也有必要为您要加载的所有游戏定义世界 width
和 height
。
就我而言:
var menuState = {
create: function(){
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.setGameSize(1000, 600);
game.scale.minWidth = 300;
game.scale.minHeight = 180;
game.scale.maxWidth = 1000;
game.scale.maxHeight = 600;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
//------------------------
game.world.width = 1000;
game.world.height = 600;
//------------------------
game.scale.updateLayout(true);
game.state.start("game");
},
start: function(){
}
}
我也遇到了类似的问题,但是我在 Google 中找到了一个解决方案,它对我有用(在我的情况下)并且会认真地重新定义世界的极限:
this.game.world.setBounds(0, 0, this.game.width, this.game.height);