cocos2dx中Hello World代码解析

前端之家收集整理的这篇文章主要介绍了cocos2dx中Hello World代码解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


本文是看了杨丰盛老师的视频(http://edu.51cto.com/course/course_id-579.html)后个人的一些感悟


杨丰盛老师说得非常详细,推荐和我一样的初学者去看一下


-------------------------------------------------------------------------------------------



AppDelegate类的代码解析


applicationDidEnterBackground()

这是个当程序进入后台时会调用的程序,有时候玩游戏时突然有一些事情,把游戏最小化,所以在这个函数中我们应该实现暂停的功能

所以这是Hello World程序中实现applicationDidEnterBackground()的代码

  1. voidAppDelegate::applicationDidEnterBackground(){
  2. CCDirector::sharedDirector()->stopAnimation();
  3.  
  4. //ifyouuseSimpleAudioEngine,itmustbepause
  5. //SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  6. }




applicationWillEnterForeground()

这是程序进入前台调用的程序,当最小化后再重新把游戏打开会调用这个函数,所以该函数应该实现继续游戏的功能所以这是Hello World程序中实现applicationWillEnterForeground()代码

  1. voidAppDelegate::applicationWillEnterForeground(){
  2. CCDirector::sharedDirector()->startAnimation();
  3.  
  4. //ifyouuseSimpleAudioEngine,itmustresumehere
  5. //SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  6. }


applicationDidFinishLaunching()

这是程序开始运行时调用函数,下面是Hello World程序中实现applicationDidFinishLaunching()代码


  1. boolAppDelegate::applicationDidFinishLaunching(){
  2. //initializedirector
  3. CCDirector*pDirector=CCDirector::sharedDirector();
  4. CCEGLView*pEGLView=CCEGLView::sharedOpenGLView();
  5. pDirector->setOpenGLView(pEGLView);
  6. //turnondisplayFPS
  7. pDirector->setDisplayStats(true);
  8. //setFPS.thedefaultvalueis1.0/60ifyoudon'tcallthis
  9. pDirector->setAnimationInterval(1.0/60);
  10. //createascene.it'sanautoreleaSEObject
  11. CCScene*pScene=HelloWorld::scene();
  12. //run
  13. pDirector->runWithScene(pScene);
  14. returntrue;
  15. }

我们知道导演是Cocos2dx中执行一切的开始,所以函数首先定义了一个导演

然后导演调用的前三个成员都是用来设置FPS,所以我们能在Hello World的左下角中看到FPS


接着该函数定义了一个场景(pScene),该场景等于HelloWorldScene中的场景,然后用导演运行这个场景,所以接下来我们就转到HelloWorldScene类中去看看这个场景是怎么实现的



HelloWorldScene类的代码解析


scene()

这是前面在AppDelegate类中applicationDidFinishLaunching()函数CCScene*pScene=HelloWorld::scene();中的scene,下面我们看看它的代码

  1. CCScene*HelloWorld::scene()
  2. {
  3. //'scene'isanautoreleaSEObject
  4. CCScene*scene=CCScene::create();
  5.  
  6. //'layer'isanautoreleaSEObject
  7. HelloWorld*layer=HelloWorld::create();
  8. //addlayerasachildtoscene
  9. scene->addChild(layer);
  10. //returnthescene
  11. returnscene;
  12. }

该场景的函数中定义了一个图层,这个图层被加在场景中,所以我们只需要对图层进行操作就可以了



init()

顾名思义这是初始化的函数

  1. boolHelloWorld::init()
  2. {
  3. //////////////////////////////
  4. //1.superinitfirst
  5. if(!CCLayer::init())
  6. {
  7. returnfalse;
  8. }
  9.  
  10. CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();
  11. CCPointorigin=CCDirector::sharedDirector()->getVisibleOrigin();
  12. /////////////////////////////
  13. //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
  14. //youmaymodifyit.
  15. //adda"close"icontoexittheprogress.it'sanautoreleaSEObject
  16. CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
  17. "CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));
  18.  
  19. pCloseItem->setPosition(ccp(origin.x+visibleSize.width-pCloseItem->getContentSize().width/2,origin.y+pCloseItem->getContentSize().height/2));
  20. //createmenu,it'sanautoreleaSEObject
  21. CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
  22. pMenu->setPosition(CCPointZero);
  23. this->addChild(pMenu,1);
  24. /////////////////////////////
  25. //3.addyourcodesbelow...
  26. //addalabelshows"HelloWorld"
  27. //createandinitializealabel
  28.  
  29. CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld","Arial",24);
  30.  
  31. //positionthelabelonthecenterofthescreen
  32. pLabel->setPosition(ccp(origin.x+visibleSize.width/2,origin.y+visibleSize.height-pLabel->getContentSize().height));
  33. //addthelabelasachildtothislayer
  34. this->addChild(pLabel,1);
  35. //add"HelloWorld"splashscreen"
  36. CCSprite*pSprite=CCSprite::create("HelloWorld.png");
  37. //positionthespriteonthecenterofthescreen
  38. pSprite->setPosition(ccp(visibleSize.width/2+origin.x,visibleSize.height/2+origin.y));
  39. //addthespriteasachildtothislayer
  40. this->addChild(pSprite,0);
  41.  
  42. returntrue;
  43. }

在这个函数中,我们看到了首先添加了一个CCMenuItemImage的按钮,这是Hello World程序中的关闭按钮,按下按钮是会调用menuCloseCallback成员函数,然后用pCloseItem->setPosition来设置按钮的位置的,最后再用this->addChild(pMenu,1)来把它添加场景中。

然后,该函数添加了一个CCLabelTTF,这是Hello World程序中显示在程序中间的那一行字,同样的,用setPosition设置位置,用this->addChild来把它添加到场景中

最后,该函数添加了一个CCSprite,这个精灵就是Hello World程序中的背景图片setPosition设置位置,用this->addChild来把它添加场景


//addChild第二个参数是添加层的意思,比如我添加了一个精灵在1,然后又添加一个在0,那么层数在1的精灵在相同的位置就会覆盖层数是0的精灵



menuCloseCallback(CCObject* pSender)

这个成员函数是但按下关闭按钮时调用的,所以他应该实现关闭程序的功能

  1. voidHelloWorld::menuCloseCallback(CCObject*pSender)
  2. {
  3. #if(CC_TARGET_PLATFORM==CC_PLATFORM_WINRT)||(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)
  4. CCMessageBox("Youpressedtheclosebutton.WindowsStoreAppsdonotimplementaclosebutton.","Alert");
  5. #else
  6. CCDirector::sharedDirector()->end();
  7. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
  8. exit(0);
  9. #endif
  10. #endif
  11. }



―――――――――――――――――――――――――――――――――――――――――――――



cocos2dx中各层次调用关系:


Director(导演)->Scene(场景)->Layer(图层)->Sprite(精灵)


导演只有一个,导演可以调用多个场景,场景中可以有多个图层,图层中也可以有多个精灵,图层中也可以有多个子图层,精灵可以不经过图层直接加在场景中。

猜你在找的Cocos2d-x相关文章