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天前
|
SQL 运维 BI
湖仓分析|浙江霖梓基于 Doris + Paimon 打造实时/离线一体化湖仓架构
浙江霖梓早期基于 Apache Doris 进行整体架构与表结构的重构,并基于湖仓一体和查询加速展开深度探索与实践,打造了 Doris + Paimon 的实时/离线一体化湖仓架构,实现查询提速 30 倍、资源成本节省 67% 等显著成效。
湖仓分析|浙江霖梓基于 Doris + Paimon 打造实时/离线一体化湖仓架构
|
13天前
|
人工智能 JavaScript 安全
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
57 13
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
|
20天前
|
机器学习/深度学习 安全 算法
十大主流联邦学习框架:技术特性、架构分析与对比研究
联邦学习(FL)是保障数据隐私的分布式模型训练关键技术。业界开发了多种开源和商业框架,如TensorFlow Federated、PySyft、NVFlare、FATE、Flower等,支持模型训练、数据安全、通信协议等功能。这些框架在灵活性、易用性、安全性和扩展性方面各有特色,适用于不同应用场景。选择合适的框架需综合考虑开源与商业、数据分区支持、安全性、易用性和技术生态集成等因素。联邦学习已在医疗、金融等领域广泛应用,选择适配具体需求的框架对实现最优模型性能至关重要。
283 79
十大主流联邦学习框架:技术特性、架构分析与对比研究
|
1月前
|
测试技术 双11 开发者
一文分析架构思维之建模思维
软件里的要素不是凭空出现的,都是源于实际的业务。本文从软件设计本源到建模案例系统的介绍了作者对于建模的思维和思考。
|
1月前
|
开发框架 前端开发 .NET
一个适用于 .NET 的开源整洁架构项目模板
一个适用于 .NET 的开源整洁架构项目模板
57 26
|
1月前
|
Java Linux C语言
《docker基础篇:2.Docker安装》包括前提说明、Docker的基本组成、Docker平台架构图解(架构版)、安装步骤、阿里云镜像加速、永远的HelloWorld、底层原理
《docker基础篇:2.Docker安装》包括前提说明、Docker的基本组成、Docker平台架构图解(架构版)、安装步骤、阿里云镜像加速、永远的HelloWorld、底层原理
362 90
|
2月前
|
机器学习/深度学习 存储 人工智能
基于AI的实时监控系统:技术架构与挑战分析
AI视频监控系统利用计算机视觉和深度学习技术,实现实时分析与智能识别,显著提升高风险场所如监狱的安全性。系统架构包括数据采集、预处理、行为分析、实时决策及数据存储层,涵盖高分辨率视频传输、图像增强、目标检测、异常行为识别等关键技术。面对算法优化、实时性和系统集成等挑战,通过数据增强、边缘计算和模块化设计等方法解决。未来,AI技术的进步将进一步提高监控系统的智能化水平和应对复杂安全挑战的能力。
|
3月前
|
监控 前端开发 数据可视化
3D架构图软件 iCraft Editor 正式发布 @icraft/player-react 前端组件, 轻松嵌入3D架构图到您的项目,实现数字孪生
@icraft/player-react 是 iCraft Editor 推出的 React 组件库,旨在简化3D数字孪生场景的前端集成。它支持零配置快速接入、自定义插件、丰富的事件和方法、动画控制及实时数据接入,帮助开发者轻松实现3D场景与React项目的无缝融合。
280 8
3D架构图软件 iCraft Editor 正式发布 @icraft/player-react 前端组件, 轻松嵌入3D架构图到您的项目,实现数字孪生
|
3月前
|
前端开发 JavaScript 测试技术
Kotlin教程笔记 - 适合构建中大型项目的架构模式全面对比
Kotlin教程笔记 - 适合构建中大型项目的架构模式全面对比
55 3
|
3月前
|
前端开发 JavaScript 测试技术
android做中大型项目完美的架构模式是什么?是MVVM吗?如果不是,是什么?
在 Android 开发中,选择合适的架构模式对于构建中大型项目至关重要。常见的架构模式有 MVVM、MVP、MVI、Clean Architecture 和 Flux/Redux。每种模式都有其优缺点和适用场景,例如 MVVM 适用于复杂 UI 状态和频繁更新,而 Clean Architecture 适合大型项目和多平台开发。选择合适的架构应考虑项目需求、团队熟悉度和可维护性。
88 6

热门文章

最新文章