工程创建:
打开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" >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)让场景对象显示在窗口中。
总结以上是内存溢出为你收集整理的cocos2d-x学习笔记(1)全部内容,希望文章能够帮你解决cocos2d-x学习笔记(1)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)