细说Cocos2d-JS——从项目构造说起
“Hello World”——多么熟悉的问候语。对于Cocos2d-JS来说,这个问候,来得太简单了,无论你是否熟悉Cocos2d-JS,只要你用过或者见过其他人使用过与Eclipse类似的IDE的话,简单几步,就可以创建一个可以运行项目,跑起你对世界的问候“Hello World”。 然而,对于大多数“新手”甚至是一部分“老手”来说,新建的一个项目,可能对各个目录或文件有个大概的了解,但,如果要深入每个文件来细致解释的话,可能就不行了,更不用说深入每一行代码了。我们今天,就来解决这个问题。 一、新建一个简单项目
二、总体分析我们首先来大概分析一下项目的组成内容,自上而下顺序分析。
下面,我们详细分析一下,其中一些比较让人困惑的部分。一些简单的文件和文件夹就不再分析了。 三、index.html——Web工程的首页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cocos2d-html5 Hello World test</title>
<link rel="icon" type="image/GIF" href="res/favicon.ico"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="full-screen" content="yes"/>
<meta name="screen-orientation" content="portrait"/>
<meta name="x5-fullscreen" content="true"/>
<meta name="360-fullscreen" content="true"/>
<style> body,canvas,div { -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; -khtml-user-select: none; -webkit-tap-highlight-color: rgba(0,0,0); } </style>
</head>
<body style="padding:0; margin: 0; background: #000;">
<canvas id="gameCanvas" width="800" height="450"></canvas>
<script src="frameworks/cocos2d-html5/CCBoot.js"></script>
<script src="main.js"></script>
</body>
</html>
代码分析:
四、main.js——与首页对应的JS
cc.game.onStart = function(){
cc.view.adjustViewPort(true);
cc.view.setDesignResolutionSize(960,640,cc.ResolutionPolicy.SHOW_ALL);
cc.view.resizeWithBrowserSize(true);
//load resources
cc.LoaderScene.preload(g_resources,function () {
cc.director.runScene(new HelloWorldScene());
},this);
};
cc.game.run();
代码分析:
五、project.json——项目配置文件
{
"project_type": "javascript","debugMode" : 1,"showFPS" : true,"frameRate" : 60,"id" : "gameCanvas","renderMode" : 0,"engineDir":"frameworks/cocos2d-html5","modules" : ["cocos2d","cocostudio"],"jsList" : [ "src/resource.js","src/app.js" ] }
代码分析:
英文介绍如下: /**
* A brief explanation for "project.json":
* Here is the content of project.json file,this is the global configuration for your game,you can modify it to customize some behavior.
* The detail of each field is under it.
{
"project_type": "javascript",// "project_type" indicate the program language of your project,you can ignore this field
"debugMode" : 1,// "debugMode" possible values :
// 0 - No message will be printed.
// 1 - cc.error,cc.assert,cc.warn,cc.log will print in console.
// 2 - cc.error,cc.warn will print in console.
// 3 - cc.error,cc.assert will print in console.
// 4 - cc.error,cc.log will print on canvas,available only on web.
// 5 - cc.error,cc.warn will print on canvas,available only on web.
// 6 - cc.error,cc.assert will print on canvas,available only on web.
"showFPS" : true,// Left bottom corner fps information will show when "showFPS" equals true,otherwise it will be hide.
"frameRate" : 60,// "frameRate" set the wanted frame rate for your game,but the real fps depends on your game implementation and the running environment.
"id" : "gameCanvas",// "gameCanvas" sets the id of your canvas element on the web page,it's useful only on web. "renderMode" : 0,// "renderMode" sets the renderer type,only useful on web : // 0 - Automatically chosen by engine // 1 - Forced to use canvas renderer // 2 - Forced to use WebGL renderer,but this will be ignored on mobile browsers "engineDir" : "frameworks/cocos2d-html5/",// In debug mode,if you use the whole engine to develop your game,you should specify its relative path with "engineDir",// but if you are using a single engine file,you can ignore it. "modules" : ["cocos2d"],// "modules" defines which modules you will need in your game,it's useful only on web,// using this can greatly reduce your game's resource size,and the cocos console tool can package your game with only the modules you set. // For details about modules definitions,you can refer to "../../frameworks/cocos2d-html5/modulesConfig.json". "jsList" : [ ] // "jsList" sets the list of js files in your game. } * */
六、config.json——模拟器配置文件
{
"init_cfg": { "isLandscape": true,"isWindowTop": false,"name": "MyNewStudy","width": 960,"height": 640,"entry": "main.js","consolePort": 6050,"uploadPort": 6060,"debugPort": 5086,"forwardConsolePort": 10088,"forwardDebugPort": 10086,"forwardUploadPort": 10090 },"simulator_screen_size": [ { "title": "iPhone 3Gs (480x320)","width": 480,"height": 320 },{ "title": "iPhone 4 (960x640)","height": 640 },{ "title": "iPhone 5 (1136x640)","width": 1136,{ "title": "iPad (1024x768)","width": 1024,"height": 768 },{ "title": "iPad Retina (2048x1536)","width": 2048,"height": 1536 },{ "title": "Android (800x480)","width": 800,"height": 480 },{ "title": "Android (854x480)","width": 854,{ "title": "Android (1280x720)","width": 1280,"height": 720 },{ "title": "Android (1920x1080)","width": 1920,"height": 1080 } ] }
代码分析:
七、resource.js——定义资源变量
var res = {
HelloWorld_png : "res/HelloWorld.png",MainScene_json : "res/MainScene.json"
};
var g_resources = [];
for (var i in res) {
g_resources.push(res[i]);
}
代码分析:
八、app.js——“Hello World”场景文件
var HelloWorldLayer = cc.Layer.extend({
sprite:null,ctor:function () {
//////////////////////////////
// 1. super init first
this._super();
/////////////////////////////
// 2. add a menu item with "X" image,which is clicked to quit the program
// you may modify it.
// ask the window size
var size = cc.winSize;
var mainscene = ccs.load(res.MainScene_json);
this.addChild(mainscene.node);
return true;
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
代码分析:
小结: 关于整个项目的大概构造分析到这里就结束了,其中一些细节我们后面还会再提到,再细致分析。我们下一篇博客,将要来分析一下,游戏启动的过程到底是怎样的! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |