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. C/C++ 安全编码 —— 指针与内存

    1. 仿踩内存 if (buf[len - 1] != 0x5A) { return; }

  2. libwebsockets 运行问题

    /****************************************************************************** * libwebsockets 运行问题 ...

  3. (七)java转译字符与连接字符串

    转义字符:通过“\”来改变后面字符的意义 \n空格 \t相当于table键 \b相当于回退 class Zyzf { public static void main(String[] args) { ...

  4. 【django】Bootstrap 安装和使用

    官网 下载:推荐下载源码包 安装Bower:使用Bower安装并管理 Bootstrap 的Less.CSS.JavaScript和字体文件(通过npm安装bower) npm install -g ...

  5. 【解题报告】牡丹江现场赛之ABDIK ZOJ 3819 3820 3822 3827 3829

    那天在机房做的同步赛,比现场赛要慢了一小时开始,直播那边已经可以看到榜了,所以上来就知道A和I是水题,当时机房电脑出了点问题,就慢了好几分钟,12分钟才A掉第一题... A.Average Score ...

  6. IDC单IP绑定多域名(包括端口)

    本文件已上传到文件列表中. ServerName 222.186.36.116:80 DocumentRoot "/home" <Directory "/home& ...

  7. java中十进制转换为任意进制

    次笔试时候遇到的关于十进制转换成十三进制的编程题. 先说说简单的思路吧: 1.十进制数 num 转换为 n进制 num%n结果肯定为n进制数的最后一位 结果存入一个数组中 2.进入一个循环num!=0 ...

  8. gson在android中的应用

    首先需要建一个实体类 Person.java 来对应json 需要注意的是实体类中的变量名必须和json传过来的key值完全一样(大小写) public class Person { private ...

  9. java 执行bat批处理文件 并关闭cmd窗口

    java 执行bat批处理文件 并关闭cmd窗口 import java.io.IOException; public class CmdMain { public static void main( ...

  10. 1056 Mice and Rice

    题意:略 思路:利用queue来模拟一轮一轮的比赛.自己第一遍做的时候完全没有用queue做的意识,代码写的贼烦最后还只得了17分,非常郁闷.通过本题反映出对queue的应用场景季度不熟悉,STL里面 ...