DegGa Posted September 10, 2016 Share Posted September 10, 2016 Hello people! I dont know what's wrong with my code, but each time i run it the console log: Uncaught ReferenceError: game is not defined index.html: Quote <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>testing</title> <script src="phaser.min.js"></script> <script src="load.js"></script> <script src="play.js"></script> <script src="player.js"></script> </head> <body> <script type="text/javascript"> window.onload = function () { var game = new Phaser.Game(640, 480, Phaser.AUTO, ''); game.state.add('load', Game.Load); game.state.add('play', Game.Play); game.state.start('load'); } </script> </body> </html> load.js: Quote var Game = {}; Game.Load = function() {}; Game.Load.prototype = { preload: function() { this.load.image('player', "assets/player.png"); }, create: function() { this.state.start('play'); } }; play.js: Quote Game.Play = function () {}; Game.Play.prototype = { create: function() { player.create(); }, update: function() { } }; player.js: Quote function Player() { }; Player.prototype.create = function() { this.player = game.add.sprite(0, 0, 'player'); }; var player = new Player(); Link to comment Share on other sites More sharing options...
Milton Posted September 10, 2016 Share Posted September 10, 2016 Hi Degga, You can never trust onload So just remove the window onload lines. I think you also need to use 'this.game.state.start' in load.js. DegGa 1 Link to comment Share on other sites More sharing options...
DegGa Posted September 10, 2016 Author Share Posted September 10, 2016 Thank you so much , but can you explain what is the different between using "window.onload()" and what you said please. Link to comment Share on other sites More sharing options...
Milton Posted September 10, 2016 Share Posted September 10, 2016 The load event fires at the end of the document loading process. So it's just a question of getting the order right. DegGa 1 Link to comment Share on other sites More sharing options...
mattstyles Posted September 10, 2016 Share Posted September 10, 2016 `game` is a local variable, local to the onload function. When you call `game.add.sprite` later (in the Player class you're trying to use) there is no local `game` so the engine will look for it globally, but there is no global reference, hence the reference error. Removing the onload closure would punt it to global, or you could do that explicitly within the function using `window.game = game` (which is an anti-pattern). You actually do expose 2 globals, namely `Game` and `player`, during the those script loads. As an added bonus for 6 there is also the issue that your wrapping onload function is totally extraneous as, currently at least, there is no more stuff to load so that script will execute only just before the onload event is triggered (possibly in the same tick as its the last thing, it can be a little tricky to tell cross-platform). lumoludo, DegGa and Milton 3 Link to comment Share on other sites More sharing options...
Milton Posted September 10, 2016 Share Posted September 10, 2016 Ah, good catch Matt. Link to comment Share on other sites More sharing options...
nazimboudeffa Posted September 10, 2016 Share Posted September 10, 2016 May it helps with this jsfiddle i have just copy pasted the code, when taping F12 i don't have any error https://jsfiddle.net/fs177uxm/1/ Link to comment Share on other sites More sharing options...
DegGa Posted September 10, 2016 Author Share Posted September 10, 2016 Thank you all guys, but Milton said: Quote I think you also need to use 'this.game.state.start' in load.js. Is there a difference between using "this.state.start()" and "this.game.state.start" ?? nazimboudeffa 1 Link to comment Share on other sites More sharing options...
Milton Posted September 10, 2016 Share Posted September 10, 2016 Maybe this thread helps a bit. DegGa 1 Link to comment Share on other sites More sharing options...
nazimboudeffa Posted September 10, 2016 Share Posted September 10, 2016 the thread you linked is very interesting, while i am reading it perhaps an image is better then many words so, i am also studying anther manner then the stages froms solarus-games.org and the image that inspire me for using "this" as a delta is this one zelda quest plus i am not sure it's sufficiant to have a correct idea of the problem btw, i am reading the thread mentioned Link to comment Share on other sites More sharing options...
DegGa Posted September 10, 2016 Author Share Posted September 10, 2016 11 minutes ago, Milton said: Maybe this thread helps a bit. Oh, thx. Link to comment Share on other sites More sharing options...
Recommended Posts