cocos2d-x学习笔记(1) 工程初始化

工程创建:

打开Windows的cmd窗口,输入一下命令



HelloWorld就是项目名称,-p(package)后面接着就是项目的包名,-l(language)后面指定语言版本,-d后面指定项目路径。

记得!!!要把Python添加到环境变量。


打开HelloWorld项目的AppDelegate.cpp文件,applicationDidFinishLaunching()函数

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::create("My Game");
        director->setOpenGLView(glview);
    }

    // turn on display FPS
    director->setDisplayStats(true);//设置是否显示游戏的帧数等调试信息

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);//设置游戏的帧数,在这里是每秒60帧

    register_all_packages();

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();//创建一个场景

    // run
    director->runWithScene(scene);//运行场景

    return true;
}


场景类的分析

HelloWorld.h头文件

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);//回调函数
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

createScene函数是不可缺的。新建一个场景类的时候是这样的:

 auto scene = HelloWorld::createScene();//创建一个场景

在Cocos2d-x 3.0里,都是通过这样的方式来创建一个场景对象,不一定要命名为scene,但在整个程序里要统一。


CREATE_FUNC(HelloWorld),CREATE_FUNC是一个宏。CREATE_FUNC是一个宏函数,具体实现如下:

#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}


和普通函数的最大区别有三处:

1)用#define定义,并且没有返回值;

2)参数没有类型,只有参数名;

3)每一行结尾处都有一个“\”符号;“\”符号是c++代码书写时用于换行的。


然后参数_TYPE_其实是宏里面的宏,因为在编译时,这个宏函数里会直接把_TYPE_出现的地方用_TYPE的值替换掉。比如之前的CREATE_FUNC(HelloWorld)。这个代码在编译时会转换为代码如下:

static HelloWorld* create(){
	HelloWorld *pRet=new HelloWorld();
	if(pRet && pRet->init()){
		pRet->autorelease();
		return pRet;
	}
	else {
		delete pRet;
		pRet=NULL;
		return NULL;
	}
}

就是把_TYPE_换成了HelloWorld。



再看createScene函数。在HelloWorldScene.cpp中可以看到createScene函数的实现:

<pre name="code" class="cpp">Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();//创建一个场景类。这个场景类并不是HelloWorld本身
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();//创建HelloWorld对象,create函数正是宏CREATE_FUNC(HelloWorld)定义的
	                                 //HelloWorld继承了Layer类, Layer不是Scene场景类。换句话说,HelloWorld并不是一个场景类
    // add layer as a child to scene
    scene->addChild(layer);//将layer对象添加到scene对象

    // return the scene
    return scene;//返回scene对象
}


 

1)通过auto scene=HelloWorld::crateScene()创建一个场景对象;

2)HelloWorld的createScene函数里面创建了一个场景对象(Scene),然后将HelloWorld对象(Layer)添加到场景对象;

3)HelloWorld对象通过crate函数创建,而create函数是由宏CREATE_FUNC(HelloWorld)来定义的;

4)通过director->runWithScene(scene)让场景对象显示在窗口中。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值