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

简介:  1 游戏逻辑架构 详细介绍 A 一个导演同一时间只能运行一个场景,场景当中,可以同时加载多个层,一个层可以可载多个精灵。层中亦可以加层。 B  场景切换 sceneàaddChild(layer); layeràaddChild(sprite);


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()方法中

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

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

4 CCApplicationProtocolCCApplicationAppDelegate)三个类的类关系介绍:

抽出代码具体实现:

优点:屏蔽了平台的差异性,实现跨平台

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 里的接口

 

目录
相关文章
|
5月前
|
消息中间件 监控 前端开发
如何开发项目管理系统中的项目结项板块?(附架构图+流程图+代码参考)
在企业项目管理中,“项目结项”是关键环节,常因流程不清、文档不全、审批滞后等问题导致交付困难。本文介绍如何通过“项目结项”模块实现线上化管理,涵盖结项申请、审批流程、成果上传、权限控制等功能,帮助团队高效完成项目收尾,避免成果丢失与流程混乱。内容包括功能设计、业务流程、系统架构、数据库设计、核心代码实现、前端交互及优化建议,助力项目管理系统快速落地并稳定运行。
|
6月前
|
人工智能 API 数据安全/隐私保护
Apifox 与 Apipost 的 API 文档引擎对比:底层架构、性能与可扩展性分析
深入探索市场上两大主流API工具——Apifox和Apipost的文档能力时,发现了令人惊讶的差距。这不仅仅是功能多寡的问题,更关乎开发效率与团队协作的质变。
|
3月前
|
Java API 开发工具
灵码产品演示:软件工程架构分析
本演示展示灵码对复杂软件项目的架构分析与文档生成能力。通过Qwen3模型,结合PlantUML,自动生成系统架构图、微服务时序图,并提取API接口文档,实现高效、智能的代码理解与文档输出。
250 5
|
3月前
|
存储 JSON 数据处理
ClkLog埋点与用户行为分析系统:架构升级与性能全面提升
随着越来越多企业在实际业务中使用 ClkLog,数据规模和分析需求也不断提升,部分用户日活已经超过10万,为了顺应这一趋势,ClkLog 秉持 “开放透明、持续演进”的理念,推出了迄今为止最重要的一次性能优化升级。新版本在大规模数据处理与复杂查询场景中,性能表现实现了跨越式提升。经过多轮研发与严格测试,新版本现已正式上线:在原有付费版 1.0 的基础上架构全面升级,并同步发布全新的 2.0 版本。为用户带来更强的性能与更广的适用场景。
|
5月前
|
存储 移动开发 JavaScript
快应用推广连接底层技术与架构以及如何结合自身系统分销的推广逻辑和技术对接-优雅草卓伊凡|果果|Ant
快应用推广连接底层技术与架构以及如何结合自身系统分销的推广逻辑和技术对接-优雅草卓伊凡|果果|Ant
114 4
快应用推广连接底层技术与架构以及如何结合自身系统分销的推广逻辑和技术对接-优雅草卓伊凡|果果|Ant
|
4月前
|
人工智能 自然语言处理 JavaScript
Github又一AI黑科技项目,打造全栈架构,只需一个统一框架?
Motia 是一款现代化后端框架,融合 API 接口、后台任务、事件系统与 AI Agent,支持 JavaScript、TypeScript、Python 多语言协同开发。它提供可视化 Workbench、自动观测追踪、零配置部署等功能,帮助开发者高效构建事件驱动的工作流,显著降低部署与运维成本,提升 AI 项目落地效率。
396 0
|
5月前
|
数据挖掘 项目管理 Python
如何开发项目管理系统中的项目启动板块?(附架构图+流程图+代码参考)
本文介绍了项目管理系统中“项目启动”板块的设计与实现,涵盖功能模块、业务流程、开发技巧及效果展示,并提供代码参考和常见问题解答,助力企业高效搭建项目管理平台。
|
4月前
|
存储 前端开发 JavaScript
如何开发设备管理系统中的经验分析报表板块 ?(附架构图+流程图+代码参考)
设备管理系统(EMS)助力企业高效管理设备生命周期,涵盖采购、维护到报废全流程。本文详解经验分析报表模块设计与开发,涵盖动态看板、点检、巡检、维修、保养及库存统计功能,提供代码示例与架构设计建议,提升设备管理效率与决策水平。
|
5月前
|
人工智能 搜索推荐 数据安全/隐私保护
快应用推广联盟分销逻辑及技术架构深度解析-优雅草卓伊凡|果果|Ant
快应用推广联盟分销逻辑及技术架构深度解析-优雅草卓伊凡|果果|Ant
165 2
|
5月前
|
缓存 Java 数据库
Java 项目分层架构实操指南及长尾关键词优化方案
本指南详解基于Spring Boot与Spring Cloud的Java微服务分层架构,以用户管理系统为例,涵盖技术选型、核心代码实现、服务治理及部署实践,助力掌握现代化Java企业级开发方案。
249 2