Global Variables (how to?)

Global Variables (how to?)
0.0 0


Can anyone help me to figure out how to write global variables??

I have a file Globals.js, inside it there is:

var ID_START = 1;
ID_END = 2;
var __ID_SPIKE = 3;

I did include Globals.js at the cocos2d appfiles

those global variables work with cocos2d-x but wont work with cocos2d-html5 (chrome).

their values are always ‘undefined’


First of all, you should never define anything in the global namespace with JavaScript. Since JavaScript does not have a linker, every object in every .js gets clobbered together. For example, say you had the following code in a .js file.

MyScene = cc.Scene.extend({
// Blah blah, you start adding things to your scene.

And later, after your game is done, you decide you need to add some advertising (or anything) to your page that leverages JavaScript and, for whatever reason, that advertisement also defines MyScene and is loaded after your script file. All of a sudden, you just lost your variable.

Now this would be very obvious if your scene was removed but what if it was for something a little less obvious. Like say one of your global variables. Those values might change from out underneath you and might take a while to find out (while all of your users are getting weird behavior).

What you should do instead is create your own ‘namespace’ to put all of your creations in. This namespace is actually just a root object, cocos2d-html5 uses ‘cc’, you could use whatever you want.

Something like this.

var myGame = {};

// Globals

myGame.__ID_START = 1;
myGame.__ID_END = 2;
myGame.__ID_SPIKE = 3;

// Scene

myGame.MyScene = cc.Scene({
// Blah

Also that should make your global variables work :).


Thanks again C.J.

I ended up doing something like this with my global vars:

TiledId = { START:1, END:2, SPIKE:3 };

it did work :slight_smile:


ps: how can you manage to write beautiful code here? “Inline Code” doesnt work for me…


I’d still stress putting everything you make into one root object to hopefully avoid headaches later on :).

To get code to highlight and format, wrap it like this.

<pre><code class=“js”>
var awesomeFormatting = {};

// Check out my awesome formatting!
awesomeFormatting.yeaboi = “yoooo![](";

var awesomeFormatting = {};

// Check out my awesome formatting)
awesomeFormatting.yeaboi =”yoooo!“;
**EDIT** I fixed this in my own example up above but also”TiledId" should probably be “tiledId” (note the first letter is not capitalized). It’s standard in javascript that objects that are newable have a capital letter to start off the name while all other names have a lowercase letter. Another one of things that can pay off in the long term :).


Yea I will do that :slight_smile:

I am porting a game from iPhone that I made a little time ago, the common components taht can be used in another games I did use a namespace for them. I still have to put the “real” game files within a namespace of the project.

I am starting to get things working, its awesome to see it in the browser and iphone :slight_smile: