I’m building a game in Cocos Creator, and also a server. I would like to be able to do some server-side checks in Node.js by re-using a few of the JavaScript objects I wrote for the game. These are not Components, but plain vanilla JavaScript objects.
It works a little bit, until I try to use any cc functions. So I tried including the Cocos2D JavaScript library like this:
if(typeof cc === 'undefined')
require("../../build/web-desktop/cocos2d-js-min");
This fails with the error:
ReferenceError: window is not defined
Which is understandable, as Node.js doesn’t have a window object. Is there a better way to do this, to include cc functions in a Node.js project?
Node.js not contains DOM so many module will not work. But you can try to require the engine/cocos2d/core/event by yourself.
To enable cc.error, cc.warn and cc.log, just modify the CCDebugger.js and require it.
Is there a way to tell the Cocos Creator compiler not to compile one line or block? I want it to execute in Node.js, but I want the Cocos Creator compiler to skip it.
I know how to make Cocos skip the code at runtime, but that’s not good enough – I’m getting compile-time errors.
EDIT: I realized that I might be looking for a preprocessor command. When Cocos Creator says “Compiled successfully” in the console, what compiler is it using, and does it have preprocessor commands?
I’m interested in hearing how this worked out for you. I’ve thought about making a physics based multiplayer game to run servers with multiple instances of CC with Box2d enabled.
I’ve done some semi successful tests by just running a Box2d library on the server and syncing it with CC but it’s extra work to have two separate coordinate systems and game objects since I’d have to code everything twice under slightly different environments.
Would it be possible to run the engine + physics not using requestAnimationFrame?