This guide focuses on how to use the single-file engine of Cocos2d-js downloaded from the online download tool.
You may have downloaded one of the following three versions:
cocos2d-jsFull version: The complete engine contains all the features and extensions of the COCOS2D-JS engine, and using this version can help you discover Cocos2d-js's amazing creativity and possibilities. You can view the list of features supported by COCOS2D-JS from the official documentation homepage.
Cocos2d-js Liteversion: Lite versions contain only the core features of COCOS2D-JS, and its advantages are stable, lightweight, and easy to use. The list of features it contains is as follows:
- Canvas renderer (WebGL not supported)
- Scenes and Layers
- Event Manager
- Timer
- Sprites and stickers
- TTF text
- Voice
- Action
- Menus and menu items
Customizedversion: If you select a custom version of the engine, the engine script file will contain all of the features you have chosen.
How to use
The single-file engine is used exactly like any other Web library, and you just need to embed the script in your page and refer to the helloworld.html in the download directory.
Note the timing of the script loading, in the sample code for HelloWorld:
window.onload = function(){ cc.game.onStart = function(){ //... }; cc.game.run("gameCanvas");};
You can see, cc.game.onStart
and both in the cc.game.run
onload function of window, because only then can the cocos2d-js-v3.0.js be guaranteed to be loaded successfully, and you can use the Cocos2d-js API in your user script. In the next section we will refer to another traditional workflow of Cocos2d-js, in which the engine script controls the order in which the scripts are loaded, so there is no need to detect the loading of the pages.
You will need to place these files on a local server or online server and access them through the server to see the correct results.
Help Links
- Online API Reference
- Download API Reference
- Document Directory
- GitHub Warehouse
- Online forum
- Online Test Example
About script compression
You can use Google Closure compiler to compress all JS files into a single file, Closure compiler advanced compression compression ratio is very high, even if you download a compact version of the engine, you can get a considerable compression ratio. The specific compression steps are as follows:
- Download closure compiler jar Program Files
- Configure Build.xml according to your environment
- Running the ant command on the console
- Delete all the JS references in the page and introduce the packaged
game.min.js
Please note that you can not write any JS script in the HTML page, all JS scripts must be packaged together, otherwise it will cause errors.
Attention
When you access the game page via the server, you will find an error message in the console: "Failed to load Resource:the Server responded with a status of 404 (Not Found)", prompting project.json
The file could not be found
This is normal, not a problem, you can ignore it. Or you can add an empty project.json
file to avoid this error report.
reprinted from:http://www.cocos2dx.net/post/241
"COCOS2D-JS Official Documents" 19, Cocos2d-js single document engine usage guidelines