温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Cocos2d-x 架构介绍

发布时间:2020-06-03 17:11:50 来源:网络 阅读:1572 作者:杜甲同学 栏目:游戏开发

Cocos2d-x  架构介绍

Cocos2d-x中主要包含以下4个部分:1、CCDirector导演  ,2、CCScene场景,3、

CCLayer层,4、CCSprite精灵



CCScene场景

场景类就是CCScene。

场景是什么?

我们来举一个例子。假设现在有一个简单的游戏

(1)首先,会有一个开场的欢迎界面。

(2)然后,会进入游戏的主界面。

由此可见,玩家玩游戏的过程就是不断地在已经预设好的界面之间根据玩家的操作进行跳转。这些构成整个流程的界面就是场景。


CCLayer层


场景是通过不同层的叠加和组合协作来实现不同功能的,所以层是我们游戏的重点大约99%以上的时间是在层上实现游戏内容。

例如:在一个场景中,最下面是背景层、其上面有菜单选项层,这就是层的叠加。



CCSprite精灵


精灵是游戏开发的主要对象,我们在游戏中的主人公、怪物都是精灵。


CCDirector导演


CCDirector导演类主要负责游戏整个过程中场景的切换等。









第二章基础知识



CCNode类

CCNode是cocosd-x的渲染链,开发游戏基本上就是和它打交道了,cocosd-x同时只能渲染一个层,因此CCScene是渲染的根节点。

CCNode几乎是游戏中大部分类的父类。



CCLabel控件

bool HelloWorld::init()
{
    bool  pRet = false;
    do {
                                                                                                            
        CC_BREAK_IF(!CCLayer::init());
//              CCLabelTTF 每次调用 setString (即改变文字)的时候,一个新的OPENGL 纹理将会被创建.。这意味着setString 和创建一个新的标签一样慢。
                                                                                                            
//          所以,当你需要频繁的更新它们的时候,尽可能的不用去使用标签对象。  而应该使用CCLabelAtlas或者是CCLabelBMFont。
        CCLabelTTF* label = CCLabelTTF::create("LabelTTF", "Marker Felt", 21);
        label->setPosition(ccp(100, 400));
        this->addChild(label);
                                                                                                            
//         CCLabelBMFont  相当于每次改变只改变了图片坐标,而CCLabelTTF要重新渲染.这个类使用之前,需要添加好字体文件,包括一个图片文件 (**.png) 和一个 字体坐标文件 (**.fnt)。
        CCLabelBMFont* label2 = CCLabelBMFont::create("CCLabelBMFont", "font_red_14.fnt");
        label2->setPosition(ccp(200, 400));
        this->addChild(label2);
                                                                                                            
//          因为帧率一直在变,使用CCLabelTTF的话效率太低,因为只是数字所以也犯不上使用 CCLabelBMFont 加载那么大的文字图像,所以使用这个比较合适。
        CCLabelAtlas* label3 = CCLabelAtlas::create("0.0", "fps_p_w_picpaths.png", 16, 24, '.');
        label3->setPosition(ccp(200, 300));
        this->addChild(label3);
                                                                                                            
        pRet =true;
    } while (0);
    return pRet;
}

代码下载: http://pan.baidu.com/share/link?shareid=112332870&uk=3189484501


界面切换

首先,创建一个C++的类,作为第二个场景。

SecondScene.h

#include "cocos2d.h"
using namespace cocos2d;
class SecondScene:public CCLayerColor
{
                                                            
public:
    CREATE_FUNC(SecondScene);
                                                            
                                                            
    virtual bool init();
                                                            
    static CCScene* scene();
                                                         
};

SecondScene.cpp

#include "SecondScene.h"
CCScene* SecondScene::scene()
{
    CCScene* scene = CCScene::create();
                                                    
    SecondScene* layer = SecondScene::create();
    scene->addChild(layer);
    return scene;
}
bool SecondScene::init()
{
    bool bRet = false;
    do {
                                                        
        CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 0)));
        CCLabelTTF* menuLabel = CCLabelTTF::create("第二个界面", "Marker Felt", 20);
        menuLabel->setColor(ccc3(255, 0, 255));
        menuLabel->setPosition(ccp(100, 300));
        this->addChild(menuLabel);
        bRet = true;
    } while (0);
    return bRet;
}




HelloWorldScene.h文件如下:

#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();
    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    void replaceScene();
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};


HelloWorldScene.cpp

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
  // 菜单类CCMenu  不能够单独使用,它是一个菜单项CCMenuItem的容器。所以我们主要研究的对象是CCMenuItem
 //CCMenuItem的常用子类有:CCMenuItemLabel和CCMenuItemSprite
    CCLabelTTF* menuLabel = CCLabelTTF::create("界面切换", "Marker Felt", 20);
                                         
     //CCMenuItemLabel创建button  第一个参数的是指将一个label对象作为参数传递进去  第二个参数是指调用方法类的指针一般为this指当前类   第三个参数是指点击按钮之后调用的方法
    CCMenuItemLabel* menuItem = CCMenuItemLabel::create(menuLabel, this, menu_selector(HelloWorld::replaceScene));
    //在创建CCMenu时,结尾一定要加上NULL,否则编译时不报错,运行时会崩溃,但是运行时会崩溃。
    CCMenu* menu = CCMenu::create(menuItem,NULL);
    menu->setPosition(ccp(100, 400));
    this->addChild(menu);
    return true;
}
void HelloWorld::replaceScene()
{
    CCDirector::sharedDirector()->replaceScene(SecondScene::scene());
                                         
}

代码下载: http://pan.baidu.com/share/link?shareid=883177378&uk=3189484501


schedule消息调度

bool HelloWorld::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!CCLayer::init());
                       
        //设置一个定时器
        this->schedule(schedule_selector(HelloWorld::stopUpdate), 1);
                       
        //开启每帧更新
        this->scheduleUpdate();
        bRet = true;  
    } while (0);
    return bRet;
}

int i =0;
void HelloWorld::update(float dt)
{
    CCLog("%d",i);
    i++;
}

void HelloWorld::stopUpdate()
{
    //停止每帧更新的方法
    this->unscheduleUpdate();
}



代码下载: http://pan.baidu.com/share/link?shareid=2828641080&uk=3189484501


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI