Phyron Posted May 3, 2015 Share Posted May 3, 2015 Hi! I make a first game using states. But I have a problem when i was have a variables. Firts in inde.html I charge the states and the game: var game = new Phaser.Game(800, 400, Phaser.CANVAS, "game");game.state.add("Preload",preload);game.state.add("TheGame",theGame);The "TheGame" code:var theGame = function(game){var pj;var tailNodes = 200;var tail = 1;var nodes = Array();var line;}theGame.prototype = { create: function(){pj = this.game.add.sprite(this.game.width/2,this.game.height/2,"pj");pj.anchor.setTo(0.5, 0.5);pj.scale.x = 0.5;pj.scale.y = 0.5;pj.angle = 90;line = new Phaser.Line(pj.x, pj.y, 0, 0);for (i=0; i<tailNodes; i++) { //ERROR this.nodes[i]={x:pj.x,y:pj.y};}},update: function(){}}Ok, in the "create" method, i use the "pj" var without any problem, but i can't use the "tailNodes" var (Uncaught ReferenceError: tailNodes is not defined). If I use this, (this.tailNodes), I don't have error, but now the error is in "nodes" var, with and without "this", like other error message. -Other question with this code is in this line: pj = this.game.add.sprite(this.game.width/2,this.game.height/2,"pj"); I need put the this for have a game var , but i dont need this for a pj var.. -And the last: I need use a canvas methods in a create or update methods, but if I use like this: canvas = game.add.graphics(0,0); he say me the same error, canvas don't exist. PD: That's crazy.. I put a value to pj var: "var pj=1". and in create, before any code i put console.log(pj). and the result issssss: Uncaught ReferenceError: pj is not defined Link to comment Share on other sites More sharing options...
stauzs Posted May 3, 2015 Share Posted May 3, 2015 hi,actually it has nothing to do with Phaser, but with javascript itself, quick answer: you probably want to use thisvar theGame = function(game){ this.pj;.....check this: http://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/and this: http://ryanmorr.com/understanding-scope-and-context-in-javascript/and another results: https://www.google.com/search?q=understanding+javascript+scope Link to comment Share on other sites More sharing options...
Recommended Posts