cocos2d-x的main函数代码很少,把一些复杂的接口封装到AppDelegate类里了,“AppDelegate”从词意可以得出是app的代理类,而一些最早的场景都会在AppDelegate类里实现。我们先看一下main.cpp

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); #ifdef USE_WIN32_CONSOLE
AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif // create the application instance
AppDelegate app; //实例化一个cocos2d_x程序
CCEGLView* eglView = CCEGLView::sharedOpenGLView(); //创建一个视口
eglView->setFrameSize(, ); //设置视口的大小 int ret = CCApplication::sharedApplication()->run(); //运行cocos2d_x的实例 #ifdef USE_WIN32_CONSOLE
FreeConsole();
#endif return ret;
}

AppDelegate类的实现如下:

class  AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate(); /**
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
//程序启动时自动进入的函数在这里我们一般初始化场景
virtual bool applicationDidFinishLaunching(); /**
@brief The function be called when the application enter background
@param the pointer of the application
*/
//程序进入后台时执行的操作
virtual void applicationDidEnterBackground(); /**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
//程序从后台到前台执行调用的函数
virtual void applicationWillEnterForeground();
};

在主函数里 实例化 AppDelegate 时会自动调用

virtual bool applicationDidFinishLaunching();
(为什么会到用它?以后会深入研究)
函数的实现:

bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); // turn on display FPS
//打开帧的显示(右下角的数字)
pDirector->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this
//设置FPS
pDirector->setAnimationInterval(1.0 / ); // create a scene. it's an autorelease object
//创建helloWorld场景
CCScene *pScene = HelloWorld::scene(); // run
//执行场景
pDirector->runWithScene(pScene);
return true;
}

helloworld的场景类如下:

class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
//但调用create成员函数式会自动执行init函数
virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer
//返回helloworld场景
static cocos2d::CCScene* scene(); // a selector callback
//关闭场景函数
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
//创建一个场景
scene = CCScene::create();
//判断场景是否创建成功
CC_BREAK_IF(! scene); // 'layer' is an autorelease object
//创建一个helloworld布景,这时会会调用init()
HelloWorld *layer = HelloWorld::create();
//判断helloworld布景是否成功
CC_BREAK_IF(! layer); // add layer as a child to scene
//添加helloworld布景到场景
scene->addChild(layer);
} while (); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); //////////////////////////////////////////////////////////////////////////
// add your codes below...
////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - , )); // Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, ); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", );
CCLabelTTF* pLabel2 = CCLabelTTF::create("My name is CharlesXue","Arial",);
CC_BREAK_IF(! pLabel2); CC_BREAK_IF(! pLabel); // Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / , size.height - ));
pLabel2->setPosition(ccp(size.width / ,size.height - )); // Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, );
this->addChild(pLabel2,);
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/, size.height/)); // Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, ); bRet = true;
} while (); return bRet;
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}

cocos2d-x 初探helloWorld的更多相关文章

  1. SpringMVC初探-HelloWorld

    MVC的概念 MVC是一种设计模式,即Model--View-Controller,模型--视图--控制器 Model(模型)表示应用程序核心(比如数据库记录列表). View(视图)显示数据(数据库 ...

  2. Cocos2d入门--3--小球运动

    本章直接上源代码.内容不难,主要就是 HelloWorldScene.h文件: #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H_ ...

  3. cocos2d ccitemimage

    #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class H ...

  4. ! cocos2d 预编译重复

    由于预编译文件重复,导致下面的类没有被编译,所以,在写代码的时候也没有提示还报错,说LoadingScene没有定义. #ifndef __HELLOWORLD_SCENE_H__ #define _ ...

  5. 1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg

     1 游戏逻辑架构 具体介绍 A 一个导演同一时间仅仅能执行一个场景,场景其中,能够同一时候载入多个层,一个层能够可载多个精灵.层中亦能够加层. B  场景切换 sceneàaddChild(la ...

  6. Cocos2d-x学习笔记(四) HelloWorld场景类

    类定义原型如下: #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" ...

  7. cocos2d-x学习知识点记录

    环境搭建 http://4137613.blog.51cto.com/4127613/751149 Cocos2d-x初探,HelloWorld解读 http://www.cnblogs.com/Ke ...

  8. cocos2d-x 中的基本概念

    在 cocos2d-x 开头配置(Windows 平台)中,介绍了新建工程,这篇就介绍下 cocos2d-x 的一些概念.(前提是需要有C++的面向对象的基本知识和C++11的常用知识) 层,场景,导 ...

  9. Cocos2d-x文本菜单

    文本菜单是菜单项只是显示文本,文本菜单类包括了MenuItemLabel.MenuItemFont和MenuItemAtlasFont.MenuItemLabel是个抽象类,具体使用的时候是使用Men ...

随机推荐

  1. InpOut32 InputTest.cpp hacking

    /************************************************************************************ * InpOut32 Inp ...

  2. python 中出现 “IndentationError: expected an indented block” 问题

    python 学习 在定义Python函数的时候如下 >>>def hello() . . .print "hello" 这样会报错的,报错如下: Indenta ...

  3. 如何快速上手.net下单元测试工具NUnit?

    NUnit基本使用 准备知识: 读此博文需要了解单元测试基本概念及NUnit的的安装. 传送门:单元测试之道(使用NUnit) 1.常见的错误 当学习一个新东西时,先学习错误,是最快的方式. 1.1 ...

  4. vector释放内存之swap方法

    相信大家看到swap这个词都一定不会感到陌生,就是简单的元素交换.但swap在C++ STL中散发着无穷的魅力.下面将详细的说明泛型算法swap和容器中的swap成员函数的使用! 1. 泛型算法swa ...

  5. Django之HttpRequest和HttpReponse

    当一个web请求链接进来时,django会创建一个HttpRequest对象来封装和保存所有请求相关的信息,并且会根据请求路由载入匹配的试图函数,每个请求的试图函数都会返回一个HttpResponse ...

  6. .NET面试题总结

    1.c#垃圾回收机制 从以下方面入手展开:  1.压缩合并算法   2.代的机制  3.GC调用终结器 2.委托和事件 先说它的定义:委托的本质是类,类型安全的指针,然后从用途上考虑,事件是包装的委托 ...

  7. 【mssql】SQL Server2012编程入门经典(第四版)(上) 读书笔记

    数据库用了很久了,但好多东西很容易忘记,这次头脑发热想起来读一遍书,做点笔记! 从第五章开始参考:<SQL Server 2005 编程入门经典>学习笔记 一.RDBMS基础:SQL Se ...

  8. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

  9. ssh面试题2

    1. BeanFactory的作用是什么? [中] BeanFactory是配置.创建.管理bean的容器,有时候也称为bean上下文.Bean与bean的依赖关系,也是由BeanFactory负责维 ...

  10. unittest之跳过用例(skip) (含如何调用类里面函数相互调取变量的方法)

    当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例也就没 ...