1
游戏逻辑架构

具体介绍

A
一个导演同一时间仅仅能执行一个场景,场景其中,能够同一时候载入多个层,一个层能够可载多个精灵。层中亦能够加层。


场景切换

sceneàaddChild(layer);

layeràaddChild(sprite);

2
项目创建命令:

A
进入tools下的project-creat

E:\Installed\cocos2d-x-2.2.3\tools\project-creator>

B

python create_project.py -project MyCocos2dx -package com.toto.mycocos01 -language cpp

C
命令解释:

-project MyCocos2dx工程名

-package com.toto.mycocos01
包名

-language cpp
开发语言可选项目有javascript lua

D
创建后的项目文件夹:

3
 简单介绍

1
查看cocos2dx游戏的版本号信息。

创建了一个cocos2dx项目之后,打开项目之后,会有例如以下项目结构

展开libcocos2d,找到cocos2d.cpp,双击打开此cpp文件,内容例如以下:

#include
"cocos2d.h"

NS_CC_BEGIN

const
char*
cocos2dVersion()

{

return
"2.2.3";

}

NS_CC_END

截图例如以下:

分析:


由上能够看出项目的版本是:2.2.3


依赖的头文件
“cocos2d.h”

2
查看程序入口

程序入口是:main.cpp

#include
"main.h"

#include
"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;   
                         //Delegate:表示
委派…为代表 n:代表

CCEGLView*
eglView =
CCEGLView::sharedOpenGLView();

eglView->setViewName("MyCocos2dx");    
            //程序的标题

eglView->setFrameSize(480,
320);                    //程序的尺寸

return
CCApplication::sharedApplication()->run();  
//关于shared的通常是单例模式

}

进入run函数,
run的代码结构例如以下(选中run(),再按F12进行查看):

int
CCApplication::run()

{

PVRFrameEnableControlWindow(false);

// Main message loop:

MSG
msg;

LARGE_INTEGER
nFreq;

LARGE_INTEGER
nLast;

LARGE_INTEGER
nNow;

QueryPerformanceFrequency(&nFreq);

QueryPerformanceCounter(&nLast);

// Initialize instance and cocos2d.

if (!applicationDidFinishLaunching())

{

return 0;

}

CCEGLView*
pMainWnd =
CCEGLView::sharedOpenGLView();

pMainWnd->centerWindow();

ShowWindow(pMainWnd->getHWnd(),
SW_SHOW);

while (1)

{

if (!
PeekMessage(&msg,
NULL, 0, 0,
PM_REMOVE))

{

// Get current time tick.

QueryPerformanceCounter(&nNow);

// If it's the time to draw next frame, draw it, else sleep a while.

if (nNow.QuadPart
- nLast.QuadPart
> m_nAnimationInterval.QuadPart)

{

nLast.QuadPart
= nNow.QuadPart;

CCDirector::sharedDirector()->mainLoop();

}

else

{

Sleep(0);

}

continue;

}

if (WM_QUIT
== msg.message)

{

// Quit message loop.

break;

}

// Deal with windows message.

if (!
m_hAccelTable || !
TranslateAccelerator(msg.hwnd,
m_hAccelTable, &msg))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

return (int)
msg.wParam;

}

程序的入口:applicationDidFinishLaunching()

AppDelegate.cpp

bool
AppDelegate::applicationDidFinishLaunching()
{

// initialize director

CCDirector*
pDirector =
CCDirector::sharedDirector();

CCEGLView*
pEGLView =
CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

// 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;

}

截图:

HelloWorldScene.h  
HelloWorld类的本质是一个层(CCLayer):

#ifndef
__HELLOWORLD_SCENE_H__

#define
__HELLOWORLD_SCENE_H__

#include
"cocos2d.h"

class
HelloWorld :
public
cocos2d::CCLayer

{

public:

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone

virtual
bool
init();

// there's no 'id' in cpp, so we recommend returning the class instance pointer

static
cocos2d::CCScene*
scene();

// a selector callback

void
menuCloseCallback(CCObject*
pSender);

// implement the "static node()" method manually

CREATE_FUNC(HelloWorld);

};

#endif
// __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include
"HelloWorldScene.h"

USING_NS_CC;

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;

}

// on "init" you need to initialize your instance

bool
HelloWorld::init()

{

//////////////////////////////

// 1. super init first

if ( !CCLayer::init()
)

{

return
false;

}

CCSize
visibleSize =
CCDirector::sharedDirector()->getVisibleSize();

CCPoint
origin =
CCDirector::sharedDirector()->getVisibleOrigin();

/////////////////////////////

// 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(origin.x
+ visibleSize.width
- pCloseItem->getContentSize().width/2
,

origin.y
+ pCloseItem->getContentSize().height/2));

// 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);

// position the label on the center of the screen

pLabel->setPosition(ccp(origin.x
+ visibleSize.width/2,

origin.y
+ visibleSize.height
- pLabel->getContentSize().height));

// 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(visibleSize.width/2
+ origin.x,
visibleSize.height/2
+ origin.y));

// add the sprite as a child to this layer

this->addChild(pSprite,
0);

return
true;

}

void
HelloWorld::menuCloseCallback(CCObject*
pSender)

{

#if (CC_TARGET_PLATFORM
== CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM
== CC_PLATFORM_WP8)

CCMessageBox("You
pressed the close button. Windows Store Apps do not implement a close button.","Alert");

#else

CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM
== CC_PLATFORM_IOS)

exit(0);

#endif

#endif

}

总结:

1、对于cocos真正的初始化是在init()方法中

2、CCScene中的 
autorelease()完毕了析构的过程

3、CCPointZero
表示的位置是CCPointMake(0,0);

4
(CCApplicationProtocol,CCApplication,AppDelegate)三个类的类关系介绍:

抽出代码详细实现:

长处:屏蔽了平台的差异性,实现跨平台

1  
CCApplicationProtocol 定义了接口

#ifndef
__CC_APPLICATION_PROTOCOL_H__

#define
__CC_APPLICATION_PROTOCOL_H__

NS_CC_BEGIN

enum
TargetPlatform

{

kTargetWindows,

kTargetLinux,

kTargetMacOS,

kTargetAndroid,

kTargetIphone,

kTargetIpad,

kTargetBlackBerry,

kTargetNaCl,

kTargetEmscripten,

kTargetTizen,

kTargetWinRT,

kTargetWP8

};

/**

* @addtogroup platform

* @{

* @js NA

* @lua NA

*/

class
CC_DLL
CCApplicationProtocol

{

public:

virtual ~CCApplicationProtocol()
{}

/**

@brief    Implement CCDirector and CCScene init code here.

@return true    Initialize success, app continue.

@return false   Initialize failed, app terminate.

*/

virtual
bool
applicationDidFinishLaunching() = 0;    
//这个类是一个纯虚函数

/**

@brief  The function be called when the application enter background

@param  the pointer of the application

*/

virtual
void
applicationDidEnterBackground() = 0;

/**

@brief  The function be called when the application enter foreground

@param  the pointer of the application

*/

virtual
void
applicationWillEnterForeground() = 0;

/**

@brief    Callback by CCDirector for limit FPS.

@interval       The time, expressed in seconds, between current frame and next.

*/

virtual
void
setAnimationInterval(double
interval) = 0;

/**

@brief Get current language config

@return Current language config

*/

virtual
ccLanguageType
getCurrentLanguage() = 0;

/**

@brief Get target platform

*/

virtual
TargetPlatform
getTargetPlatform() = 0;

};

// end of platform group

/// @}

NS_CC_END

#endif   
// __CC_APPLICATION_PROTOCOL_H__


CCApplication 各个平台不同的逻辑


AppDelegate 私有继承了CCApplication
仅实现CCApplicationProtocol
里的接口

1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg的更多相关文章

  1. Django 创建web项目之HelloWorld

    Django.Flask.Tornado并称为python WEB三大框架.Diango是一个开源的web应用框架,具有开发速度快的特点.同时因为过度封装,具有性能低的特点. 创建Django项目,启 ...

  2. 四、IntelliJ IDEA 之 HelloWorld 项目创建及相关配置文件介绍

    咱们通过创建一个 Static Web 项目大致了解了 IntelliJ IDEA 的使用界面,接下来,趁着这个热乎劲,咱们来创建第一个 Java 项目“HelloWorld”,进入如下界面: 如上图 ...

  3. 关于eclipse创建Maven项目创建的问题

    1.问题: 为什么Maven Update Project JDK变回1.5 解释:官方文档 The Compiler Plugin is used to compile the sources of ...

  4. tfs中如何创建团队项目及如何操作团队项目

    创建团队项目集合 tfs server管理控制台\团队项目集合页面.选择'创建集合'链接,按向导即可创建项目集合. 创建团队项目 创建好团队项目集合后,就要开始创建团队项目了. 进入vs,连接上tfs ...

  5. 分模块创建maven项目(一)

    maven是一个项目构建和管理的工具. 我们可以通过maven仓库可以实现管理构建(主要是JAR还包括:WAR,ZIP,POM等等). 我们可以通过maven插件可以实现编译源代.产生Javadoc文 ...

  6. IOS5基础教程之一-----如何创建XCode项目

    一.IOS的基础知识 1.只有一个应用程序正在运行.在IOS上,每一段时间内只能激活一个应用程序并在屏幕上显示. 2.只有一个窗口.只允许应用程序操作的一个窗口. 3.访问受限.只能在IOS为应用程序 ...

  7. m2eclipse简单使用,创建Maven项目 ,运行mvn命令(转)

    前面介绍了如何安装m2eclipse,现在,我们使用m2ecilpse导入Hello World项目. 选择菜单项File,然后选择Import,我们会看到一个Import对话框,在该对话框中选择Ge ...

  8. .Net Core .Net Core V1.0 创建MVC项目

    .Net Core V1.0 创建MVC项目 创建MVC项目有两种方式: 一.创建Web项目:(有太多没用的东西要去删太麻烦) 2.项目目录结构: 此种方法要注意的是,会创建好多个json文件,下面就 ...

  9. Django 从0开始创建一个项目

    title: Django 从0开始创建一个项目 tags: Django --- Django 从0开始创建一个项目 创建Django工程及配置 创建工程:django-admin starproj ...

随机推荐

  1. 安卓Menu键的问题

    近期开发中有须要Menu键,结果发现了一个非常尴尬的问题.我的測试机上有Menu键.可是測试平板上没有,队友的測试机上竟然也没有Menu键.这着实有些尴尬... 上网谷歌之后才发现问题所在: 仅仅有在 ...

  2. ACdream 1083 有向无环图dp

    题目链接:点击打开链接 人民城管爱人民 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Othe ...

  3. 7. 稀疏表示之OMP,SOMP算法及openCV实现

    一.前言 稀疏表示是自上世纪90年代开始,从人眼的视觉感受野获得启示,逐渐被人们所研究.现在已经发展为一种重要的信息表示方法.所谓稀疏表示是指,一个信号在过完备字典中,可以由少数个原子线性表达, 其数 ...

  4. .net DataTable 正确排序姿势

    关于dataTable中根据列排序正确姿势做个随笔,方便查阅 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Ad ...

  5. 关于object在使用上的问题

    关于object在使用上的问题 1.直接调用标签<object>中的单击事件 <object>是含有onclick和ondblclick两个事件的.按照以往方式,直接在里面调用 ...

  6. linux基础内容学习一:linux下的分区及安装

    linux看系统版本信息 uname -a 如果显示为i386,i686则为32位系统,如果为x86_64则为64位 一块硬盘最多可以有四个主分区其中一个主分区可以用一个扩展分区替换,在这个扩展分区中 ...

  7. PHP学习笔记二十九【接口】

    <?php //定义接口 //接口可以定义属性,但必须是常量而且是public //接口的所有方法必须是public interface Iusb{ public function start( ...

  8. UI开发学习中遇到的问题汇总

    1.给UIView设置圆角,边框,阴影绘制,需要使用layer 1)设置圆角cornerView.layer.cornerRadius = 20; //设置试图圆角的大小cornerView.laye ...

  9. (转)ubuntu下如何查看软件安装目录以及安装版本

    1.查询版本 aptitude show 软件名 例如:aptitude show kde-runtime 显示如下: ****@ubuntu:~$ aptitude show kde-runtime ...

  10. (原)mkl用到的函数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5585301.html 计算 $C=\alpha *A*B+\beta *C$: void cblas_ ...