PhaserEditor2D / PhaserEditor

A friendly IDE to develop HTML5 games based on the Phaser framework.
https://phasereditor2d.com
Eclipse Public License 1.0
330 stars 45 forks source link

"Uncaught ReferenceError: game is not defined" - And other basic stuff. #116

Closed Blue-Pilkinton-Ching closed 4 years ago

Blue-Pilkinton-Ching commented 5 years ago

Hi Everyone! I'm really new to phaser, and coding in general, I only know a few bits and pieces of different languages and stuff.

I have been trying to figure out how to make a simple, space ships destroy astroid game Using Phaser Editor 1.5.3 and ARCADE PHYSICS. But I have quickly run into a problem. Whenever I try to add velocity or anything like that to my game. I get this weird error outputted in the inspect element console. It said,

Uncaught ReferenceError: game is not defined at Level.create (Level.js:44) at Phaser.StateManager.loadComplete (phaser.js:30083) at Phaser.Loader.finishedLoading (phaser.js:74233) at Phaser.Loader.processLoadQueue (phaser.js:74190) at Phaser.Loader.asyncComplete (phaser.js:74263) at Phaser.Loader.fileComplete (phaser.js:75120) at Image.file.data.onload (phaser.js:74522)

I tried to copy other examples and things, but a lot of them seem to use a different structure in the coding. With less State/Canvas/Js files. It all just seems to be coded in one large file. These are my files:

Index.html:

<!DOCTYPE HTML>

Astroids

Main.js:

window.onload = function() { var game = new Phaser.Game(800, 600, Phaser.AUTO);

// Add the States your game has.
// game.state.add("Boot", Boot);
// game.state.add("Menu", Menu);
// game.state.add("Preload", Preload);
game.state.add("Level", Level);

game.state.start("Level");

};

Leve.jsl

// -- user code here --

/ --- start generated code --- /

// Generated by 1.5.3 (Phaser v2.6.2)

/**

}

/* @type Phaser.State / var Level_proto = Object.create(Phaser.State.prototype); Level.prototype = Level_proto; Level.prototype.constructor = Level;

Level.prototype.init = function () {

this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.scale.pageAlignHorizontally = true;
this.scale.pageAlignVertically = true;
this.stage.backgroundColor = '#ffffff';

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

};

Level.prototype.preload = function() {

this.load.pack('graphics', 'assets/pack.json');

};

Level.prototype.create = function(){

var _player = this.add.sprite(400.0, 500.0, 'playerShip1_blue');
_player.anchor.setTo(0.5, 0.5);
game.physics.enable(_player, Phaser.Physics.ARCADE);

this._player.body.velocity.x = 600;

};

/ --- end generated code --- / // -- user code here --

Level.prototype.update = function() {

};

---------------------

Aside from all that I was wondering what actually executes the functions: Create, Preload, Init, Update, and Render. What their main purpose is, (especially the last three) and what the whole "prototype" thing is about.

Thanks so much for your time Blue

PhaserEditor2D commented 4 years ago

Please re-open if you continue getting this problem.