cocos2d-x 初探helloWorld
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的更多相关文章
- SpringMVC初探-HelloWorld
MVC的概念 MVC是一种设计模式,即Model--View-Controller,模型--视图--控制器 Model(模型)表示应用程序核心(比如数据库记录列表). View(视图)显示数据(数据库 ...
- Cocos2d入门--3--小球运动
本章直接上源代码.内容不难,主要就是 HelloWorldScene.h文件: #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H_ ...
- cocos2d ccitemimage
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class H ...
- ! cocos2d 预编译重复
由于预编译文件重复,导致下面的类没有被编译,所以,在写代码的时候也没有提示还报错,说LoadingScene没有定义. #ifndef __HELLOWORLD_SCENE_H__ #define _ ...
- 1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg
1 游戏逻辑架构 具体介绍 A 一个导演同一时间仅仅能执行一个场景,场景其中,能够同一时候载入多个层,一个层能够可载多个精灵.层中亦能够加层. B 场景切换 sceneàaddChild(la ...
- Cocos2d-x学习笔记(四) HelloWorld场景类
类定义原型如下: #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" ...
- cocos2d-x学习知识点记录
环境搭建 http://4137613.blog.51cto.com/4127613/751149 Cocos2d-x初探,HelloWorld解读 http://www.cnblogs.com/Ke ...
- cocos2d-x 中的基本概念
在 cocos2d-x 开头配置(Windows 平台)中,介绍了新建工程,这篇就介绍下 cocos2d-x 的一些概念.(前提是需要有C++的面向对象的基本知识和C++11的常用知识) 层,场景,导 ...
- Cocos2d-x文本菜单
文本菜单是菜单项只是显示文本,文本菜单类包括了MenuItemLabel.MenuItemFont和MenuItemAtlasFont.MenuItemLabel是个抽象类,具体使用的时候是使用Men ...
随机推荐
- docx转doc时,防止公式被转成图片的解决办法
编辑社回复需要doc(Word 97-2003)格式的文档,可是将docx(Word 2007+)另存为doc格式时,发现公式被转成了图片.其实,最简单的办法就是,打个电话过去给编辑社:“大爷,拜托您 ...
- leetcode_sql_4,196
196. Delete Duplicate Emails Write a SQL query to delete all duplicate email entries in a table name ...
- 一起来看CORE源码(一) ConcurrentDictionary
先贴源码地址 https://github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/System/Col ...
- c#代碼小集
一.字符串[Uri]轉換出RouteData private RouteData UriToRouteData(Uri uri) { var query = uri.Query; ) { query ...
- ZenCoding[Emmet]語法簡介【轉】
快速指南 下面是一些常用的Zen Coding功能,目前VS2013的Web Essentials插件已经支持. '#' 创建一个id特性 '.' 创建一个类特性 '[]' 创建一个自定义特性 '&g ...
- Maven打包同一个jar有不同的:版本+时间戳(解决思路)
在我们的开发过程中,目前流行的版本控制工具maven,在项目开发阶段,大家都是通过发布SNAPSHOT快照版进行相互模块之间的依赖开发, 这个时候就会有一个问题,要是一天构建多次的快照版,会发现在项目 ...
- idea的有些路径问题
resource文件在xml的路径 http://www.cnblogs.com/zqr99/p/7642712.html resource文件在io流的读取 this.getClass().getR ...
- 开启mac terminal 命令/路径自动补全功能
用惯了windows命令行工具的按Tab自动补全路径功能后,在mac terminal上敲命令很不习惯.其实mac terminal也有这个功能. 在命令行输入nano .inputrc 进入.inp ...
- MySQL连接查询、联合查询、子查询
参考地址:http://blog.csdn.net/u011277123/article/details/54863371 1.MySQL连接查询 连接查询:将多张表(>=2)进行记录的连接(按 ...
- Python学习之异常处理
1.首先了解错误和异常的概念: 错误:代码运行前的代码错误或者是程序执行过程中的逻辑错误 1:语法错误:代码不符合解释器或者编译器语法(代码错误) 2:逻辑错误:不完整或者不合法输入或者计算出现问题( ...