Jump to content

Black screen when using var create = function() { };


Rudrabhoj Bhati
 Share

Recommended Posts

When I use

var 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  :lol:  Any idea why this happened?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...