1 游戏逻辑架构

详细介绍

A 一个导演同一时间只能运行一个场景,场景当中,可以同时加载多个层,一个层可以可载多个精灵。层中亦可以加层。

B  场景切换

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

截图如下:

分析:

A  由上可以看出项目的版本号是:2.2.3

B  依赖的头文件 “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__

2  CCApplication 各个平台不同的逻辑

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

1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg相关推荐

  1. vs窗体应用linux,使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)...

    使用Visual Studio2019创建C#项目(窗体应用程序.控制台应用程序.Web应用程序) 一.VS的开发环境 首先你得安装了vs2019,然后确认下下面三个组件是否存在,如果没有要下载一下. ...

  2. bean加载context idea_02-基于IDEA创建SpringBoot项目并进行入门分析

    SpringBoot 项目创建 创建Module 基于IDEA创建项目Module,模块名为04-springboot-start,组id和包名为com.cy,如图所示: 填写module信息,如图所 ...

  3. vue项目结构php写哪里,Vue项目的创建,以及项目目录结构的分析

    Vue项目环境搭建""" node ~~ python:node是用c++编写用来运行js代码的 npm(cnpm) ~~ pip:npm是一个终端应用商城,可以换国内源 ...

  4. 怎么创建python django项目_创建Django项目图文实例详解

    本文实例讲述了创建Django项目的方法.分享给大家供大家参考,具体如下: 创建Django项目 创建一个HelloDjango项目 GitHub地址:https://github.com/liang ...

  5. 【Unity游戏开发】Android中如何集成Unity3D项目——入门级踩坑

    最近的学习之路真的是波折,先是想学Kotlin,结果赶上了算法比赛,恶补了几天算法,然后回归Kotlin,这周又需要调研一下Unity3D,需要把Unity项目嵌入到我们的Android项目中. 今天 ...

  6. 创建Django项目 目录结构 Url介绍 路由配置

    创建Django项目 文章目录 创建Django项目 创建项目的指令 Django项目的目录结构 `settings.py` 文件介绍 URL 介绍 URL定义 Django如何处理一个URL对应的请 ...

  7. TFS创建团队项目(三)

    打开Visual Studio 2013,视图-团队资源管理器-连接图标(插头图标) 当前是没有TFS服务器,点击服务器按钮 添加,并在URL地址栏里输入装有TFS的服务器IP地址(配置完TFS后有这 ...

  8. SpringBoot之创建SpringBoot项目(idea开发)

    方法一:通过maven创建 maven项目创建成功后(之前写过,指路[maven创建java web项目]),只需在pom.xml里面添加springboot作父级程序包即可. <!--父级引入 ...

  9. Python工作笔记007---win10安装Python3.7.3_以及用pycharm创建Python项目_以及对System interpreter理解

    技术交流QQ群[JAVA,C++,Python,.NET,BigData,AI]:170933152 1.pycharm安装以后如果,本地不安装Python是不可以用的, 2.win10安装Pytho ...

最新文章

  1. 计算机二级各个科目的作用,计算机二级考试的各个科目的内容及区别.doc
  2. 速读《构建之法:现代软件工程》提问
  3. 不可逆的类初始化过程
  4. 京东云上集市谋定中国农民丰收节交易会 建九江电商产业园
  5. web模块化规范和实现
  6. python编程8g的内存够么_详解解决Python memory error的问题(四种解决方案)
  7. Apache Subversion 1.7.2发布,开源版本控制工具
  8. 模仿Spring实现一个类管理容器
  9. Asp.net下from认证统一认证配置
  10. JavaScript 中 call()、apply()、bind() 的用法
  11. Nginx Upload Module 上传模块
  12. 初中计算机考试操作题免费,初中信息技术考excel操作题.doc
  13. 健全营销体制是打造营销生态系统的命脉
  14. Simulink Resolver 旋转变压器解码仿真
  15. 为什么创业?创业能给你带来什么?
  16. 金山云发布感知评价指标KPA 拨开遮挡高清视频技术的迷雾
  17. 运筹学 知识点总结 (七)
  18. 相机计算坐标公式_相机采样点的坐标转换方法与流程
  19. 科技论文插图软件总结
  20. mos 多路模拟电子开关_C-MOS开关的单端输入模拟多路调制器-CD4051

热门文章

  1. 5.Java中的常量以及常量的分类
  2. wxWidgets:wxDC类用法
  3. wxWidgets:wxCloseEvent类用法
  4. boost::system::error_code相关的测试程序
  5. boost::safe_numerics::checked_result相关的测试程序
  6. boost::process::spawn相关的测试程序
  7. boost::parameter::aux::maybe相关的测试程序
  8. boost::mp11::mp_clear相关用法的测试程序
  9. boost::hana::detail::any_of用法的测试程序
  10. boost::core_numbers用法的测试程序