
@H_502_0@本文是看了杨丰盛老师的视频(http://edu.51cto.com/course/course_id-579.html)后个人的一些感悟 @H_502_0@
@H_502_0@杨丰盛老师说得非常详细,推荐和我一样的初学者去看一下 @H_502_0@
@H_502_0@------------------------------------------------------------------------------------------- @H_502_0@
@H_502_0@
@H_502_0@AppDelegate类的代码解析 @H_502_0@
@H_502_0@applicationDIDEnterBackground() @H_502_0@这是个当程序进入后台时会调用的程序,有时候玩游戏时突然有一些事情,把游戏最小化,所以在这个函数中我们应该实现暂停的功能, @H_502_0@所以这是Hello World程序中实现applicationDIDEnterBackground()的代码
voIDAppDelegate::applicationDIDEnterBackground(){CCDirector::sharedDirector()->stopAnimation();//ifyouuseSimpleAudioEngine,itmustbepause//SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();} @H_502_0@@H_502_0@
@H_502_0@
@H_502_0@applicationWillEnterForeground() @H_502_0@这是程序进入前台时调用的程序,当最小化后再重新把游戏打开会调用这个函数,所以该函数应该实现继续游戏的功能,所以这是Hello World程序中实现applicationWillEnterForeground()的代码
voIDAppDelegate::applicationWillEnterForeground(){CCDirector::sharedDirector()->startAnimation();//ifyouuseSimpleAudioEngine,itmustresumehere//SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();} @H_502_0@@H_502_0@applicationDIDFinishLaunching() @H_502_0@这是程序开始运行时调用的函数,下面是Hello World程序中实现applicationDIDFinishLaunching()的代码 @H_502_0@
@H_502_0@
boolAppDelegate::applicationDIDFinishLaunching(){//initializedirectorCCDirector*pDirector=CCDirector::sharedDirector();CCEGLVIEw*pEGLVIEw=CCEGLVIEw::sharedOpenGLVIEw();pDirector->setopenGLVIEw(pEGLVIEw);//turnondisplayFPSpDirector->setdisplayStats(true);//setFPS.thedefaultvalueis1.0/60ifyoudon'tcallthispDirector->setAnimationInterval(1.0/60);//createascene.it'sanautoreleaSEObjectCCScene*pScene=HelloWorld::scene();//runpDirector->runWithScene(pScene);returntrue;} @H_502_0@我们知道导演是Cocos2dx中执行一切的开始,所以该函数首先定义了一个导演。@H_502_0@然后导演调用的前三个成员都是用来设置FPS,所以我们能在Hello World的左下角中看到FPS @H_502_0@
@H_502_0@接着该函数定义了一个场景(pScene),该场景等于HelloWorldScene中的场景,然后用导演运行这个场景,所以接下来我们就转到HelloWorldScene类中去看看这个场景是怎么实现的 @H_502_0@
@H_502_0@
@H_502_0@@H_301_156@HelloWorldScene类的代码解析 @H_502_0@@H_301_156@
scene()
这是前面在AppDelegate类中applicationDIDFinishLaunching()函数中CCScene*pScene=HelloWorld::scene();中的scene,下面我们看看它的代码
CCScene*HelloWorld::scene(){//'scene'isanautoreleaSEObjectCCScene*scene=CCScene::create();//'layer'isanautoreleaSEObjectHelloWorld*layer=HelloWorld::create();//addlayerasachildtoscenescene->addChild(layer);//returnthescenereturnscene;} 该场景的函数中定义了一个图层,这个图层被加在场景中,所以我们只需要对图层进行 *** 作就可以了
@H_502_0@@H_502_0@
@H_502_0@init() @H_502_0@顾名思义这是初始化的函数
boolHelloWorld::init(){////////////////////////////////1.superinitfirstif(!cclayer::init()){returnfalse;}CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();CCPointorigin=CCDirector::sharedDirector()->getVisibleOrigin();///////////////////////////////2.addamenuitemwith"X"image,whichisclickedtoquittheprogram//youmaymodifyit.//adda"close"icontoexittheprogress.it'sanautoreleaSEObjectCcmenuItemImage*pCloseItem=CcmenuItemImage::create("Closenormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));pCloseItem->setposition(ccp(origin.x+visibleSize.wIDth-pCloseItem->getContentSize().wIDth/2,origin.y+pCloseItem->getContentSize().height/2));//createmenu,it'sanautoreleaSEObjectCcmenu*pMenu=Ccmenu::create(pCloseItem,NulL);pMenu->setposition(CCPointZero);this->addChild(pMenu,1);///////////////////////////////3.addyourcodesbelow...//addalabelshows"HelloWorld"//createandinitializealabelcclabelTTF*pLabel=cclabelTTF::create("HelloWorld","Arial",24);//positionthelabelonthecenterofthescreenpLabel->setposition(ccp(origin.x+visibleSize.wIDth/2,origin.y+visibleSize.height-pLabel->getContentSize().height));//addthelabelasachildtothislayerthis->addChild(pLabel,1);//add"HelloWorld"splashscreen"CCSprite*pSprite=CCSprite::create("HelloWorld.png");//positionthespriteonthecenterofthescreenpSprite->setposition(ccp(visibleSize.wIDth/2+origin.x,visibleSize.height/2+origin.y));//addthespriteasachildtothislayerthis->addChild(pSprite,0);returntrue;} @H_502_0@在这个函数中,我们看到了首先添加了一个CcmenuItemImage的按钮,这是Hello World程序中的关闭按钮,按下按钮是会调用menuCloseCallback成员函数,然后用pCloseItem->setposition来设置按钮的位置的,最后再用this->addChild(pMenu,1)来把它添加到场景中。 @H_502_0@然后,该函数添加了一个cclabelTTF,这是Hello World程序中显示在程序中间的那一行字,同样的,用setposition设置位置,用this->addChild来把它添加到场景中 @H_502_0@最后,该函数还添加了一个CCSprite,这个精灵就是Hello World程序中的背景图片,用setposition设置位置,用this->addChild来把它添加到场景中 @H_502_0@@H_502_0@//addChild第二个参数是添加层的意思,比如我添加了一个精灵在1,然后又添加一个在0,那么层数在1的精灵在相同的位置就会覆盖层数是0的精灵 @H_502_0@
@H_502_0@
@H_502_0@menuCloseCallback(CCObject* pSender) @H_502_0@这个成员函数是但按下关闭按钮时调用的,所以他应该实现关闭程序的功能
voIDHelloWorld::menuCloseCallback(CCObject*pSender){#if(CC_TARGET_PLATFORM==CC_PLATFORM_WINRT)||(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)CcmessageBox("Youpressedtheclosebutton.windowsStoreAppsdonotimplementaclosebutton.","Alert");#elseCCDirector::sharedDirector()->end();#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)exit(0);#endif#endif} @H_502_0@@H_502_0@――――――――――――――――――――――――――――――――――――――――――――― @H_502_0@
@H_502_0@
@H_502_0@cocos2dx中各层次调用关系: @H_502_0@
@H_502_0@Director(导演)->Scene(场景)->Layer(图层)->Sprite(精灵) @H_502_0@
@H_502_0@导演只有一个,导演可以调用多个场景,场景中可以有多个图层,图层中也可以有多个精灵,图层中也可以有多个子图层,精灵可以不经过图层直接加在场景中。 总结
以上是内存溢出为你收集整理的cocos2dx中Hello World代码解析全部内容,希望文章能够帮你解决cocos2dx中Hello World代码解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)