我在用相器做游戏。我正在加载一个背景图像,它的信息(文件位置)存储在一个JSON文件中。当我尝试加载它时,背景是黑色和空的,在控制台中我得到:
Phaser.Cache.getImage:在缓存中找不到"background0“键。
下面是我代码的相关摘录:
function create() {
//>Load JSON file and background images found inside the file
$.getJSON("levels.json", function(json) {
for (var i = 0; i < json.levels.length; i++) {
game.load.image('background' + i.toString(), json.levels[i].background);
}
game.load.start();
});
back_layer = game.add.group();
var i = 0;
var level_finished = 0;
$.getJSON("levels.json", function(json) {
if (i < json.levels.length) {
console.log("Level " + (i + 1).toString());
var current_background = back_layer.create(0, 0, 'background' + i.toString());
check = setInterval(function() {
if (level_finished == 1) {
i++;
current_background.destroy();
clearInterval(check);
}
}, 500)
}
});
}下面是JSON文件:
{"levels":[
{
"background": "assets/img/Back.png",
"portals": [
{
"locationX": 400,
"locationY": 450,
"toX": 100,
"toY": 200,
"spinSpeed": 1
},
{
"locationX": 50,
"locationY": 200,
"toX": 100,
"toY": 450,
"spinSpeed": 2
}
]
}
]}使用Chrome、Firefox和Opera进行测试,每次打开页面时,它似乎都会随机出现错误,或者加载后台并正常工作。我正在使用WAMP本地主机的网页。
发布于 2017-01-04 13:36:19
加载资产(JSON、映像等)的相位方法是在game.load.*函数(或您为此指定的函数中)中使用preload API。在您的情况下,代码应该是:
// Use your game instance here
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
function preload() {
// Load JSON file describing the level
game.load.json('levels', 'levels.json');
}
// The function below will be automatically invoked by Phaser when
// the assets in the preload() function finished loading
function create() {
var json = game.cache.getJSON('levels');
// Enque the load of the background images found inside the level file
for (var i = 0; i < json.levels.length; i++) {
game.load.image('background' + i.toString(), json.levels[i].background);
}
// Specify loadComplete() as a callback to be called when all assets finished loading
game.load.onLoadComplete.add(loadComplete, this);
// Load the newly enqued assets
game.load.start();
}
// The function below will be automatically invoked by Phaser when
// the assets in the create() function finished loading
function loadComplete() {
var json = game.cache.getJSON('levels');
back_layer = game.add.group();
var i = 0;
var level_finished = 0;
if (i < json.levels.length) {
console.log("Level " + (i + 1).toString());
var current_background = back_layer.create(0, 0, 'background' + i.toString());
check = setInterval(function() {
if (level_finished == 1) {
i++;
current_background.destroy();
clearInterval(check);
}
}, 500)
}
}之所以有随机行为(有时运行良好,有时不工作)是因为您使用jQuery ($.getJSON())而不是Phaser内置系统来加载JSON文件。
由于jQuery与Phaser无关,所以它们不同步(调用期间也没有排序)。因此,有时$.getJSON()以足够快的速度加载JSON文件,以便在调用Phaser的create()方法时做好准备。在这种情况下,一切都如预期的那样工作。当$.getJSON()不够快时,将在加载JSON文件之前调用create(),从而导致错误。
发布于 2017-01-01 11:22:51
在Phaser中加载资产是异步的,如果在请求加载后立即尝试访问某个资产,它将以这种方式失败。将预加载逻辑移到preload() (与create()类似的另一种Phaser.State方法)- Phaser将确保在调用create()方法时,preload()中请求的所有内容都已加载。
https://stackoverflow.com/questions/41408766
复制相似问题