Jump to content

Variables


davidhetherington
 Share

Recommended Posts

Hello,

 

I have been using flash for teaching games development and have taken the plunge into html 5, very impressed with phaser which I plan to use as a replacement. My background is programming where I have always declared variables in advance, I have been doing this in Phaser but have noticed if I took the variable declaration out for example 'var score;' the game still works fine. Should I be declaring variables at the top although as I create the score in the game it must generate the variable?

 

regards

 

David

 

ps Love Phaser! 

Link to comment
Share on other sites

Where you declare them basically just defines their scope. So if you declare them at the top they become 'global', visible from within any function. If you declare them within a function they are local to that function only, not visible outside of it. Hence why both are valid, it's just creating a different sort of var in the process.

Link to comment
Share on other sites

Hello Rich,

Thank you for your speedy reply :-) I understand about local and global variables. however if for example I removed the line of code that declares the variable ie var score; and but include a line score = 0; no error is thrown? Am I right in assuming score = 0; is declaring the variable and initialising it? Would it be preferable to use the line var score = 0; ? I would just like clarification as I want to introduce the use of phaser in my games development class rather than using flash as I'm really impressed with your games engine.

Many thanks

David

Link to comment
Share on other sites

Yes, if you haven't declared a variable when it gets initalised, the variable is declared as a global variable on the spot then initalised.

This is great if you forget to declare a variable as your code will still run but this can easily introduce bugs.

An example of this is a hero object and a creature object each with a health variable. If you don't declare the variables locally and the hero gets a powerup which raises his health, the creature's health will raise too; beacuse there's only one health variable and both objects are using it.

A great way to check for these easy mistakes is to imput your code into jshint (lots of plugins or just use www.jshint.com).

For instance if i put in:

for (i = 0; i < 5; i++)
{
     alert(i);
}


It will spit out that my code has an undeclared variable and will show the lines that i'm using it.

Link to comment
Share on other sites

Sorry if I'm teaching Grandma to suck eggs, but this is JavaScript behaviour rather than Phaser. If you put var at the front it creates the variable right there in that scope (i.e. inside a function, or where-ever you've done it). If you miss out 'var' it starts looking up through the various scope layers above it until it hits 'window', if it hasn't found one by then it creates a new var on window, so sort of like a global var except it can be deleted (true global vars can't). The difference is that if it finds a var matching it while going up the scope chain it stops there and uses that scope.

 

In short: prefix with var, always, unless you're doing some serious js-scoping-foo.

Link to comment
Share on other sites

No that is great, I was just after some clarification regarding the declaration of variables as I want to create some tutorials based on a game I am developing. check out, http://www.goliathnet.co.uk/games for the games I have created although I have encrypted the code. The sound does not appear to work on IE though.

 

Thank you for all your help

 

regards

 

David 

Link to comment
Share on other sites

Yes it would. I would recommend looking in the resources/Project Templates folder in the repository, you will find some examples of how to break a project up into classes and how to define 'global' vars that are game bound (rather than polluting the global space in the browser).

Link to comment
Share on other sites

I have been using the resources in the Project Template folder which have been great to help me create the states. I think I need to look closely at refining the use of the variables and minimising the use of global variables. When using the variables you always have to put 'this.' before the variable in the use of states.

 

I also have problems with my timer and use it as follows in Phaser 1.1.5 , it works but I want it to output the seconds rather than having to set a separate variable  which increases on the timertick:

 

create

timerTick = this.time.create(false);

timerTick.loop(1000, this.updateTimer, this);
timerText.content = 'Time: ' + this.seconds;
timerTick.start();
 
 
//Updates timer and outputs to the screen
updateTimer: function () {
this.seconds++;
timerText.content = 'Time: ' + this.seconds;
},
 
Im sure there must be a more elegant way to achieve this.
 
regards
 
David
 
ps Only been using Phaser for a week in my spare time, so impressed with how well structured and easy it is to use. 
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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