Rudrabhoj Bhati Posted November 22, 2013 Share Posted November 22, 2013 When I usevar create = function() { };instead of:function create () {}I get black screen in phaser games. For example, see what happens when I do so in Alien Invaders like game included with phaser:http://i.imgur.com/V6GBscZ.png Just a day or so ago I started to think JS isn't that bad lol. Probably I was wrong Any idea why this happened? Link to comment Share on other sites More sharing options...
Vidsneezes Posted November 22, 2013 Share Posted November 22, 2013 I think if you want to use a function expression you are going to have to give it a namevar _create = function create(){} Link to comment Share on other sites More sharing options...
Rudrabhoj Bhati Posted November 22, 2013 Author Share Posted November 22, 2013 I think if you want to use a function expression you are going to have to give it a namevar _create = function create(){}That gives a white screen, canvas isn't rendered at all :Shttp://i.imgur.com/S6d9ca8.png Link to comment Share on other sites More sharing options...
powerfear Posted November 22, 2013 Share Posted November 22, 2013 This is a var hoisting issue, I would suggest you looking that up. Basically since the game is the first line executed you're only passing it an empty variable because the function is not declared yet only the variable. When using direct function like function create the whole function is hoisted at the top so it works fine. To fix it simply make the new Phaser.Game the last line or use normal function like it already does. en4ce 1 Link to comment Share on other sites More sharing options...
Rudrabhoj Bhati Posted November 22, 2013 Author Share Posted November 22, 2013 This is a var hoisting issue, I would suggest you looking that up. Basically since the game is the first line executed you're only passing it an empty variable because the function is not declared yet only the variable. When using direct function like function create the whole function is hoisted at the top so it works fine. To fix it simply make the new Phaser.Game the last line or use normal function like it already does.Thanks it works perfectly now! I am looking into http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-javascript-hoisting-explained/ Thanks again or help Link to comment Share on other sites More sharing options...
triptych Posted November 22, 2013 Share Posted November 22, 2013 Sounds like a good candidate for a FAQ entry, if there is a FAQ. Link to comment Share on other sites More sharing options...
Recommended Posts