espace Posted July 22, 2016 Share Posted July 22, 2016 hi, i'm beginnin with js and phaser and I have some questions related to this snippet "the game.js" ( it's based on a tutorial from Emmanuel Ferrato). var theGame = function(game){ spriteNumber = null; number = 0; workingButtons = true; higher = true; } theGame.prototype = { create: function(){ number = Math.floor(Math.random()*10); spriteNumber = this.game.add.sprite(160,240,"numbers"); var higherButton = this.game.add.button(160,100,"higher",this.clickedHigher,this); higherButton.anchor.setTo(0.5,0.5); clickedHigher: function(){ higher=true; this.tweenNumber(true); }, tweenNumber: function(higher){ } }, } normally when we declare a variable we write : var myVariable = somestuff here,it's write spriteNumber = null; without var and with = null > why ? for wich reason ? next the clickedHigher : function() wath is the signification of this ? what does it refer ? thanks for your lights Link to comment Share on other sites More sharing options...
espace Posted July 23, 2016 Author Share Posted July 23, 2016 Nobody? Link to comment Share on other sites More sharing options...
fitness23 Posted July 23, 2016 Share Posted July 23, 2016 Writing var only makes the variable available in its local scope where if you don't write var it makes it available throughout the global scope. Without seeing the entire code it's hard to judge why he did this but my best guess is that he's using that variable throughout the game code. As for why it's equal to null depending on when it's being used in the game it might need that name to be available right from the start even if it's equal to nothing, just so long as it exists. Link to comment Share on other sites More sharing options...
fitness23 Posted July 23, 2016 Share Posted July 23, 2016 As for the "clickedHigher" bit, since this has been written in a prototype format, he's had to write "this." To get to that function. Without it he would have been trying to call that function within the "create" function which of course doesn't exist. It's all to do with context within the game object. Hope that helps Link to comment Share on other sites More sharing options...
espace Posted July 24, 2016 Author Share Posted July 24, 2016 Thanks a lot for this explanation. It helps me a lot. Have a good day Link to comment Share on other sites More sharing options...
Recommended Posts