Will Posted September 28, 2013 Share Posted September 28, 2013 Hello again I'm new to JS in general, but I understand some of it (programmed a lot before).I find I learn best by doing, hence making a game with phaser! In all the examples, the code in inside this - (function () { //Some code})();What is the significance of the function? Works without it! Oh yes, and if I wanted to have multiple files (player.js, enemy.js ETC) how would I go about doing this? Also, how would I change the background colour of the game? Link to comment Share on other sites More sharing options...
onedayitwillmake Posted September 28, 2013 Share Posted September 28, 2013 That is what is referred to as an immediate function.You start with a function,Function(){}Then immediately wrap it in parenthesis to make it an expression, like you might a math operation 2*(1+3) (Function(){})Then finally you call it like you always call a function, using () so that everything inside of it is evaluated right away.(Function(){})()---It has several benefits, ill like a couple of key ones. It prevents any variables declared within it from leaking into the global scope (Window). It also allows you to create local variables in them that are private to the objects you create inside that function. Scoping is a big pain in JavaScript, something they definitely got wrong - so we have to do things like this to help side step some if the common problems plicatibu and Will 2 Link to comment Share on other sites More sharing options...
Will Posted September 28, 2013 Author Share Posted September 28, 2013 That is what is referred to as an immediate function.You start with a function,Function(){}Then immediately wrap it in parenthesis to make it an expression, like you might a math operation 2*(1+3)(Function(){})Then finally you call it like you always call a function, using () so that everything inside of it is evaluated right away.(Function(){})()---It has several benefits, ill like a couple of key ones. It prevents any variables declared within it from leaking into the global scope (Window). It also allows you to create local variables in them that are private to the objects you create inside that function.Scoping is a big pain in JavaScript, something they definitely got wrong - so we have to do things like this to help side step some if the common problemsI see! Thanks! Link to comment Share on other sites More sharing options...
rich Posted September 29, 2013 Share Posted September 29, 2013 game.stage.backgroundColor sets the game background. Give it any hex value like you would in html/css. I'll be posting up some examples of making a game using States and multiple files shortly. Link to comment Share on other sites More sharing options...
Recommended Posts