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: paddle is undefined (Firefox)
Uncaught TypeError: Cannot read property 'x' of undefined (Chrome)
I also have a warning:
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();
}
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.