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 ...
随机推荐
- Unity3d 背景、音效 播放 简单demo
仅实现功能,AudioListener在MainCamera中 using UnityEngine; using System.Collections; using System.Collection ...
- Image Pyramid (二)
上一篇文章里,我们介绍了图像金字塔的基本原理,就是一种分层次的下采样.这篇文章里我们简单介绍一下图像金字塔的一种应用,image blending.利用图像金字塔做 image blending,可以 ...
- I.MX6 fbset 使用
/****************************************************************************** * I.MX6 fbset 使用 * 说 ...
- Centos6.4_X64编译安装php-5.4.17、nginx-1.4.2、mysql-5.6.13
安装参考: CentOS 6.3编译安装Nginx1.2.2+MySQL5.5.25a+PHP5.4.5 http://www.dedecms.com/knowledge/servers/linux- ...
- Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
使用Eclipse启动Tomcat时出现启动超时的问题如下所示: Server Tomcat v7.0 Server at localhost was unable to start within 4 ...
- Java JDK安装和配置(Windows)
安装和配置JDK JDK中自带了JRE,不需要单独下载, 打开JDK安装, 选择安装目录,下一步,装完JDK,会问是否安装JRE,选下一步, 最后还会问是否安装Java FX, 装完后就全部完成了JD ...
- 关于android方向传感器的使用
Android2.2以后 orientation sensors 就被deprecated了 官方建议用acceleration and magnetic sensor 来算 关于这个问题,CSDN上 ...
- further occurrences of HTTP header parsing errors will be logged at DEBUG level.
1. 获取参数Json的值为null String json=request.getParameter("Json"); 首先检查是否有下面的东东, 信息: Error par ...
- 蓝桥杯 算法训练 ALGO-108 最大的体积
算法训练 最大体积 时间限制:1.0s 内存限制:256.0MB 问题描述 每个物品有一定的体积(废话),不同的物品组合,装入背包会战用一定的总体积.假如每个物品有无限件可用,那么有些体积是永 ...
- (转)oracle - type
本文转载自:http://www.cnblogs.com/o-andy-o/archive/2012/05/25/2517741.html type定义: oracle中自定义数据类型oracle中有 ...