My game uses Cocos2d HTML5 2.1. I was using the default jsloader to load all scripts. Today I tried the closure compiler and compiled the cocos2d sources along with my source code into a single js file. When I used the compiler in “simple” mode, it worked fine and the single file was around 600 KB. I tried using advanced mode and the file turns out to around 350 KB, which is great. However, my app fails with an error.
I’ve narrowed the error onto the _super invocation in the Application constructor (In what used to be my main.js). If I comment this line, the app goes a little further, but then fails since the super constructor was not initialized.
I’ve fixed this issue. Seems I was having trouble with advanced mode due to the following two reasons
I had multiple elements in my build.xml. I unified everything into one
I get a lot of data using ajax and was using dot notation to access properties within this data. Now changed to obj[“prop”] notation wherever I have external data and the code works fine after compiling.
Sorry for the late reply. Are you using whitespace mode or advanced mode for compilation?
One thing I notice in your file is that you are missing any extern declarations.
You need to specify all js files that you refer, but is not included in as . Plus cocos2d has an extern file that you would need to include. However, if you use whitespace compilation mode, I don’t think this will be an issue
.. Any other files you refer, but not part of compiled js
Another thing that comes to mind is that the order in which source files are mentioned is important. So, if you refer some javascript function or variable defined in another file, it should ideally come earlier in the tags.
Looking at your error, you are trying to invoke a function in some part of your code, but it cannot be resolved and is substituted by “undefined”. So if you are using whitespace mode, it could get fixed just by changing the order of files correctly. If it’s in advance mode, then the problem could be that externs are missing (In advanced mode most symbols are replaced).
The most common problem for me was that I was using some data defined elsewhere. I have an object I get from a REST service.
data = {firstName “Hari”, lastName “G”}; console.log(data.firstName);
This had to be replaced with
data[“firstName”]
Another possible problem is that you are passing some data as a javascript object and don’t quote the keys, so they get replaced
i.e. {firstName: “Hari”} might need to be {firstName “Hari”}. Closure compiler will retain quoted names.
One way of debugging would be to use a debugger like firebug and say “Break on all exceptions”. Then you can possible find the area where your code is breaking and perhaps see the callstack. Another option is to put in console.log() statements in your code to identify where the problem exactly lies.
Actually the compilation seems to be fine… The problem is that it is unable to identify ‘cc’ when it is referred for the first time in the obfuscated code…
I excluded the js file that holds all the configuration parameters for cocos2d-HTML5 and also adds an event listener to initiate the loading of all the js files of the game. Then I added the obfuscated js file (game.js) as the only element in appFiles array.
This solved the issue of ‘cc is not defined’ but now, I got “Assertion failed: CCApplication ctor”