上一章(http://www.jb51.cc/article/p-veeasfti-ho.html)中我们讲解了mac下的cocos2dx环境的搭建,并且创建了helloworld项目,这一章我们就来解读这个helloworld项目。
1.用xcode打开我们的项目,会得到如下图的目录结构:
在这里我们重点关注mac目录和classes目录,打开了一个mac项目,我们第一反应肯定是去看mac下的目录:
mac目录下有两个代码文件,我们看到main.cpp就知道,这是项目的入口处了。
2.main.cpp文件的解读:
打开main.cpp,我们可以看到main.cpp的源码如下:
/**************************************************************************** Copyright (c) 2010 cocos2d-x.org http://www.cocos2d-x.org Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "AppDelegate.h" #include "cocos2d.h" USING_NS_CC; int main(int argc,char *argv[]) { AppDelegate app; return Application::getInstance()->run(); }
可以看出main函数是构建了appdelegate项目并运行它,在classes目录下我们可以找到这个文件(进过查找资料,我们可以知道自定义的项目源码放在classes目录)。
3.AppDelegate.cpp源码解析:
#include "AppDelegate.h" //引入头文件 #include "HelloWorldScene.h" //引入helloworldsecene的文件,该类主要是创建场景和层的将在下一步讲解该类 USING_NS_CC; //定义命名空间为cocos2d,这样引用类和方法时比较方便,其源码为:#define USING_NS_CC using namespace cocos2d AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } bool AppDelegate::applicationDidFinishLaunching() {//应用开始时所做的工作 // initialize director auto director = Director::getInstance(); //创建一个导演,关于导演,场景,层和精灵的关系将在文章最后讲到 auto glview = director->getOpenGLView();//该导演打开了gl的端口 if(!glview) { glview = GLView::create("My Game"); //创建为 my game director->setOpenGLView(glview); //将创建的设置进去 } // turn on display FPS director->setDisplayStats(true);//true显示fps // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60);//每秒显示的fps数 // create a scene. it's an autorelease object auto scene = HelloWorld::createScene();//得到场景,该场景是从helloworld中创建的 // run director->runWithScene(scene);//运行这个场景 return true; } // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() {//应用在后台运行时所做的工作 Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine,it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() {//应用被唤醒时所做的工作 Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine,it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); }
4.HelloWorldScene.cpp文件的解析:
#include "HelloWorldScene.h" USING_NS_CC;//定义命名空间为cocos2d,这样引用类和方法时比较方便,其源码为:#define USING_NS_CC using namespace cocos2d Scene* HelloWorld::createScene()//创建场景的方法 { // 'scene' is an autorelease object auto scene = Scene::create(); //创建场景 // 'layer' is an autorelease object auto layer = HelloWorld::create();//创建层,由头文件:class HelloWorld : public cocos2d::Layer可以看出该类自己就是一个层 // add layer as a child to scene scene->addChild(layer);//将层加入到场景中 // return the scene return scene;//返回场景 } // on "init" you need to initialize your instance bool HelloWorld::init()//init方法系统会自动去调用 { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image,which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2)); // create menu,it's an autorelease object auto menu = Menu::create(closeItem,NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu,1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World","Arial",24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label,1); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite,0); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }源码先简单的解析到这里,下面来阐述导演,场景,层和精灵的关系,阐述完后我们在下一章中自己定义场景的时候就可以很好的理解了。
5.导演,场景,层与精灵的阐述:
游戏和戏剧的区别就是游戏里你是半个编剧而戏剧里你只是一个观众,游戏你可以参与,戏剧你只能观看。何为半个编剧,即是你能够按照已经设定好的规则来指挥演员来演出。
游戏里的导演和戏剧的导演一样,就是按照剧本来指挥演员等元素来呈现一个画面,音乐等。我们以演出《雷雨》为例。
导演就是根据《雷雨》的剧本来导出所有的视觉听觉等五官效果。
场景就是《雷雨》里某一幕,拿侍萍控诉周朴园来说,这一幕就是一个场景。
层就是场景里的3维关系,在这里,天空乌云密布,天空为一层,院子里花草摇曳,院子为一层,大厅里侍萍控诉周朴园,大厅为一层,这些层组成了一幕。
精灵就是演员了,之所以称为精灵是因为这些演员不全是人。在天空这一层,乌云是精灵,他们有动作,有形态。在院子里花,草,树木都是精灵,它们也有各自的动作和形态。大厅里侍萍和周朴园也是精灵。
由此可以看出,我们写游戏的时候不妨把自己看成一个拿着半个剧本在拍戏的剧作人。下一章我们将创建自己的场景并运行它。