Jump to content

a silly noob question.


Recommended Posts

So i did the phaser tutorial and expanded on to add a few platformer qualities.


So i learned a lot in the process (basicly that i dont know much).


Now that i am trying to understand how to run the game from a .js file vs the html file. I am taking the javascript class at code academy to understand it better. The mainly thing i noticed from running in JS is that you create a game container in the index.html


Then i would load phaser into it and then load the "first level' or state.


However i noticed that i get the an error just about every line that unexpected "game" not defined and so on with the other variables.


I looked at a few examples and came to the undetstand "this." is used. The syntax seems to be different running from a js file. Could someone point me in the right direction to learn about this?


I would believe its better to run the game from the js file correct? 


Thanks in Advance.

Link to comment
Share on other sites

You'll always run a Phaser game from a html file. In the examples a single js file sets up and runs. I think your second way is running a game that has been broken up into states. These still get initiated from within a html file though. The method I am referring to here is shown in the phaser templates. Have a look at index.html.

The concept of scope is what you need to look up regarding the "this" keyword. In js I have an imperfect understanding of scope which probably won't improve as Phaser is so good at helping with it, but in lieu of another reply by an expert here's some of what I have learned:

if you look at the top of the game.js state in the templates (I presume you are using these - they're in the resources folder if not) you'll see a bunch of lines like this.game, this.sound etc. These give you access to these objects from anywhere within the state. Normally, in js, a variable is scoped to the function in which it is created. If you try to call it from outside it will fail,as it is undefined from the point of view of that function.- it's scope is that function.

Tthese properties listed above are very useful! Unfortunately they do mean that for all of the examples and many on line tutorials, you do have to change any references to these things to have a this in front of them if you want to apply them to your own project using States. For anything other than a very small project or experiment you should use some way of breaking up your project into seperate files. There a number of different ways to do this.

I follow the pattern of the templates as they do just as I want in making menu, title, games screens etc easy to control. In the create function I create any variables I want accessible to the rest of the state eg this.numberOfLives = 3; For variables that are only useful within a function, and only until that function is finished define them like this: var tempNumber = 2; that will then be cleaned up by the garbage collector and save leaking memory. Make sure you use var though as without it, it will work but the variable will permanently persist and waste memory.

It is advised to learn about scope but don't feel you must be absolutely perfect in your understanding - when not using phaser I get it to work through trial and error sometimes and the use of the bind command - with Phaser you can develop your understanding more gradually and I never need to bind. When something is reported as undefined it is either s typo or a problem with scope (at least it seems that way to me.). Just be aware that many of Phasers methods also use "this" as a parameter and that is to keep the scope to the calling function. This is the reason my limited understanding is not a barrier for me!

My recommendation is to start building something using the templates, maybe turn one of the example games into a multi state game, ie give tanks a title screen and a credits page. Find where you need to add 'this' and how it all comes together. You'll be surprised how much you can do without comprehensive understanding which you can then build while making something fun. Good luck.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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