Cocos2d-x 2.0 从HelloWorld入手
来源:互联网 发布:linux 全部删除 编辑:程序博客网 时间:2024/05/20 19:27
从上一篇《Cocos2d-x 2.0 在Windows平台下的使用》已经初步了解了Cocos2d-x的安装、编译,也已经可以运行HelloWorld示例了,运行HelloWorld至少所需的文件,包括:素材、动态库,我们把"...\Release.win32"里的"HelloWorld.exe"单独拷贝到一个新文件夹,至少所需的文件如下图所示:
打开cocos2d-win32.vc2008.sln,展开"HelloWorld"工程,工程结构如下所示:
对应"...\HelloWorld"文件夹的结构如下所示:
可知"Classes"放置游戏开发逻辑相关、平台无关的代码,"Resources"放置图片、声音、配置等资源,"win32"等放置平台相关的代码。
了解如何运行,在win32下就从main()函数开始,打开"main.cpp",代码如下所示:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "main.h"
#include "../Classes/AppDelegate.h"
#include "CCEGLView.h"
USING_NS_CC;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
CCEGLView& eglView = CCEGLView::sharedOpenGLView();
eglView.setViewName("Hello World");
eglView.setFrameSize(480, 320);
// set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line.
// eglView.setDesignResolutionSize(480, 320);
return CCApplication::sharedApplication().run();
}
#include "../Classes/AppDelegate.h"
#include "CCEGLView.h"
USING_NS_CC;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
CCEGLView& eglView = CCEGLView::sharedOpenGLView();
eglView.setViewName("Hello World");
eglView.setFrameSize(480, 320);
// set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line.
// eglView.setDesignResolutionSize(480, 320);
return CCApplication::sharedApplication().run();
}
其中AppDelegate是最主要的类,代理运行着整个应用程序,它的体系如下:
简化流程如下所示:
而CCEGLView是整个程序窗口类,游戏的显示、表现等等都在这上面进行,跟AppDelegate合起来的流程如下:
在CCApplication::run()中调用了applicationDidFinishLaunching()函数,代码如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
// pDirector->enableRetinaDisplay(true);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
// pDirector->enableRetinaDisplay(true);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
/////////////////////////////
// 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
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize();
// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 50) );
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) );
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
return true;
}
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
/////////////////////////////
// 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
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize();
// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 50) );
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) );
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
return true;
}
在这个函数里,创建了各个子元素,创建完之后,用addChild加入到主层上,上面的注释清晰的描述了步骤。
参考阅读:
1.cocos2d-x初探学习笔记(1)--HelloWorld http://blog.csdn.net/bill_man/article/details/7202458
2.cocos2d-x之HelloWorld范例分析(一) http://www.lugw.net/?p=80005
- Cocos2d-x 2.0 从HelloWorld入手
- Cocos2d-x 2.0 从HelloWorld入手
- 入手cocos2d-x
- Cocos2d-x 剖析HelloWorld
- cocos2d-x HelloWorld基本原理
- Cocos2d-x HelloWorld初见
- cocos2d-x之helloworld
- Cocos2d-x 经典HelloWorld
- cocos2d-x HelloWorld 分析
- cocos2d-x Helloworld 详解
- cocos2d-x helloworld 分析
- Cocos2d-x之helloworld
- Cocos2d-x之helloworld
- Cocos2d-x之HelloWorld
- Cocos2d-x 之 HelloWorld
- 编译Cocos2d-x的HelloWorld
- [cocos2d-x] -- 新建HelloWorld工程
- [cocos2d-x] -- 新建HelloWorld工程 .
- Gxt和GWT整合小示例
- linux内核部件分析之——waitqueue与线程的阻塞
- linux shell编程2 - 引号、转义
- dfd圧%^%
- 程序助手
- Cocos2d-x 2.0 从HelloWorld入手
- SAP RFC
- 七夕在相爱人的眼中也是普通的一天 。
- 并行处理
- js sleep函數
- 计算代码块执行时间
- I2C设备地址的设定
- mysql-定制mysql工具连接提示符
- 餐桌上的“爱心菜”