Cocos2d-x HelloWorld 之源码分析
发布时间:2020-12-14 21:40:44 所属栏目:百科 来源:网络整理
导读:在《HelloWorld 之目录结构》中我们已经了解了怎么样新建一个HelloWorld工程,下面我们来看看源代码是什么样子的 main.cpp -- 程序的主入口 #include "main.h" "AppDelegate.h" "cocos2d.h" USING_NS_CC ; int APIENTRY _tWinMain ( HINSTANCE hInstance , H
在《HelloWorld 之目录结构》中我们已经了解了怎么样新建一个HelloWorld工程,下面我们来看看源代码是什么样子的
main.cpp-- 程序的主入口
问题:AppDelegate app; 的时候创建应用程序对象,但是为什么可以使用Application::getInstance()->run(); 来运行呢。为什么不是app.run(); 呢?
AppDelegate.cpp-- 应用程序类,相当于Android 里面自定义Application
classAppDelegate:privatecocos2d::Application
classCC_DLLApplication:publicApplicationProtocol
Application 的单例实现方案
win32
Application *sm_pSharedApplication = 0; //0 就是null,C++比Java灵活多了Application::Application() //构造函数: _instance(nullptr _accelTable _instance GetModuleHandle _animationInterval.QuadPart; CC_ASSERT(! sm_pSharedApplication sm_pSharedApplication this;}Application::~Application() //析构函数this==} Application() //拿到实例函数sm_pSharedApplication}
Android
;Application() CCAssert"" NULL}
可以看出win32 和Android版的基本相同,在整个Cocos2d-x中AppDelegate就是一个单例模式。
解上面的问题:单例模式和Java里面的有点不同,不是类本身实例化自己,还是要靠外部来实例化,例如在main.cpp 里面的
AppDelegate app;
创建了实例,同时赋值给静态变量
sm_pSharedApplication,以后可以通过getInstance() 方法拿到这个实例(C++里面是叫实例吗?好纠结这些问题哦),为了满足单例模式拿到实例的一贯使用getInstance() 方法(习惯了Java里面叫方法,好像C++里面叫函数,以后我会改的)拿到实例。所以不用app->run();明白了吗?
回到AppDelegale.cpp/h
相关的函数
AppDelegate();virtual~AppDelegatevoid initGLContextAttrs(); //初始化OpenGL Context 的6个属性,red,green,blue,alpha,depth,stencilbool applicationDidFinishLaunching(); //当应用程序启动的时候执行,这里是游戏的开始,例如启动了loading 界面,开始加载游戏 applicationDidEnterBackground(); //当游戏进入后台时会执行,例如电话来了 applicationWillEnterForeground(); //当游戏恢复到前台运行时候执行,例如拒接电话
实现的函数
英文不好也要看英文呀
这里又有一个问题,Context 是不是和Android 的Context 同一个概念呢?但是我连Android 的Context 的不理解,就算相同我也不知道是怎么样的一个东西。
说明几点
applicationDidFinishLaunching()// initialize directorauto director Director();//导演隆重登场,这里也是用了单例模式,整个游戏只能有一个导演,不知道有没有副导演呢? glview director->getOpenGLView//GLView 再次出现,还是导演亲自请来的ifglview){ //0(null) 是假,非0 是真,那当什么时候!glview 为真呢?就是当glview == null 的时候 //当glview 为null的时候glview GLViewImplcreate("My Game"setOpenGLView}// turn on display FPSsetDisplayStatstrue); //显示FPS// set FPS. the default value is 1.0/60 if you don't call thissetAnimationInterval(1.0/60); //刷新屏幕的间隔时间,就是等待多久换一次帧数据 register_all_packages// create a scene. it's an autorelease object scene HelloWorldcreateScene// runrunWithScenescene}
HelloWorld 的超简单实现了另外两个函数
// This function will be called when the app is inactive. When comes a phone call,it's be invoked tooapplicationDidEnterBackgroundstopAnimation// if you use SimpleAudioEngine,it must be pause// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();}// this function will be called when the app is active againapplicationWillEnterForegroundstartAnimation// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();}
从分析上面的代码看出,显示的第一个场景为HelloWorld::createScene();
HelloWorldScene.cpp-- 运行的场景,相当于Android的Activity
相关函数
//there's no 'id' in cpp,so we recommend returning the class instance pointer
函数实现
Scene Scene(); //创建一个场景 layer (); //HelloWorld是一个层,MD,才发现HelloWorld 是一个层,看文件名字,我还以为是场景呢 sceneaddChildlayer); //直接显示HelloWorld 层}
总要知识点
// on "init" you need to initialize your instance//////////////////////////////// 1. super init firstfalse Size visibleSize getVisibleSize();//显示大小 Vec2 origin 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 closeItem MenuItemImage"CloseNormal.png""CloseSelected.png" CC_CALLBACK_1menuCloseCallback)); closeItemsetPosition(Vec2origin.x + visibleSizewidth -getContentSize().width/2 originy height2// create menu,it's an autorelease object menu MenucloseItem menuZEROmenu1); //添加菜单// 3. add your codes below...// add a label shows "Hello World"// create and initialize a label label LabelcreateWithTTF"Hello World""fonts/Marker Felt.ttf"// position the label on the center of the screen labelheight // add the label as a child to this layerlabel); //添加label// add "HelloWorld" splash screen" sprite Sprite"HelloWorld.png"// position the sprite on the center of the screen spritevisibleSizexy// add the sprite as a child to this layersprite);//最后加入一个精灵当做背景}
回调函数,直接退出程序
(Refend();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit);#endif}
定制HelloWorld -- win32 版
//定制HelloWorld//弄了背景图 background "custom_hello_world.jpg" backgroundbackground//显示个Label 标题,显示在背景图的上面 labelTitle "Custom Hello World" labelTitlelabelTitle//使用Label 做点击菜单项 labelMenu "Click Me,Click Me" closeMenu MenuItemLabellabelMenu closeMenu labelMenusetCallbackCC_CALLBACK_1)); //设置回调函数closeMenuNULL}
运行结果,nice
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |