首次加载游戏未定义变量时出现相位器错误 [英] Phaser error on first load of game undefined variables

查看:85
本文介绍了首次加载游戏未定义变量时出现相位器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Google Chrome和Firefox中运行游戏时,游戏屏幕为黑色,刷新后即可正常运行.

When I run my game in Google Chrome and Firefox, the game screen is black, once I refresh it, it runs as it should.

当屏幕为黑色时,我在控制台中看到的错误是:

The error I see in the console when the screen is black is:

TypeError:桨未定义(Firefox)

TypeError: paddle is undefined (Firefox)

未捕获的TypeError:无法读取未定义(Chrome)的属性"x"

Uncaught TypeError: Cannot read property 'x' of undefined (Chrome)

我也有一个警告:

Phaser.Loader-取消/重置活动加载(Firefox和Chrome)

Phaser.Loader - active loading canceled / reset (Firefox & Chrome)

我的代码的相关部分是:

The relevant part of my code is:

var game = new Phaser.Game(800, 560, Phaser.AUTO, 'phaser-canvas', { preload: preload, create: create, update: update });

function setUpLevel(i) {
    $.getJSON("levels.json", function(json) {
            paddle.x = json.levels[i].paddle_startX;
    });
}

function processPaddle() {
    var paddle_loc = paddle.x + 80
}

function preload() {    
    //>Game assets
    game.load.image('paddle', 'assets/img/Paddle.png');

    // Load JSON file describing the level
    game.load.json('levels', 'levels.json');
}

//Paddle
var paddle;
var paddle_vel;

var json;

// The function below will be automatically invoked by Phaser when
// the assets in the preload() function finished loading

function create() {
    game.load.reset(true);

    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() {
    json = game.cache.getJSON('levels');

    game.physics.startSystem(Phaser.Physics.ARCADE);

    paddle = mid_layer.create(100, 400, 'paddle');
    game.physics.enable(paddle, Phaser.Physics.ARCADE);

    paddle.scale.setTo(0.7, 0.7);
    paddle.body.immovable = true;
    paddle.body.collideWorldBounds = true;

    setUpLevel(current_level);    
}

 function update() {
        processPaddle();
 }

我猜错误是因为loadComplete()函数尚未完成,并且update()函数已经启动,它使用了paddle变量,但尚未分配值-但是我没有当然.

I guess the error is because the loadComplete() function hasn't finished yet, and the update() function has started, which uses the paddle variable, which hasn't been assigned a value yet - But I'm not sure.

推荐答案

update函数将在create完成后立即开始触发,但是在您这种情况下,桨叶不存在,因此会出现错误开始被抛出.我建议您将游戏分解为多个状态,启动"状态可以加载json +预加载器资产,然后可以切换到预加载器"状态,以提取所需的所有资产(从json读取) .完成后,您可以进入游戏状态或类似状态.这样也可以使您的东西(逻辑上)保持清洁.

The update function will start firing as soon as create completes, but in your case the paddle doesn't exist by that point, so errors will start being thrown. I would suggest you break your game down into States, a 'Boot' state could load your json + preloader assets, and then you can swap to a 'Preloader' state which pulls in all the assets you need (as read from the json). Upon completion you can move to a Game state or similar. It would help keep things cleaner (logically) for you as well.

这篇关于首次加载游戏未定义变量时出现相位器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆