It is really easy to make small games with cocos2dx-js. But while project scale getting larger, there are 2 things would make you crazy.
- All the classes defined in global scope, you should always take care when name a new class.
jsList
in project.json is more and more difficult to maintain.
Then it’s time to make cocos2dx-js project modulization.
Here are the steps of how to make cocos2dx-js modulization with webpack2.
1. Create cocos2dx-js project
cocos new CocosJSWebpack -p com.jmengxy.ccwebpack -l js
2. Add webpack to project
Create package.json
Create webpack.config.js
3. Make changes to project
Open project.json and change jsList
to
Replace content of main.js with:
Create init.js in /src
Rename /src/app.js to /src/HelloWorldScene.js
Add import res from './resource';
as first line and export default HelloWorldScene;
as last line to HelloWorldScene.js
Replace content of /src/resource.js to
4. Every thing done
Then you can type npm start
in your command line.
A few seconds later, you would see the image below, congratulations!
Enjoy modulization of cocos2dx-js now.
Github Repo