本文是看了杨丰盛老师的视频(http://edu.51cto.com/course/course_id-579.html)后个人的一些感悟
杨丰盛老师说得非常详细,推荐和我一样的初学者去看一下
-------------------------------------------------------------------------------------------
AppDelegate类的代码解析
applicationDidEnterBackground()
这是个当程序进入后台时会调用的程序,有时候玩游戏时突然有一些事情,把游戏最小化,所以在这个函数中我们应该实现暂停的功能,
所以这是Hello World程序中实现applicationDidEnterBackground()的代码
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
applicationWillEnterForeground()
这是程序进入前台时调用的程序,当最小化后再重新把游戏打开会调用这个函数,所以该函数应该实现继续游戏的功能,所以这是Hello World程序中实现applicationWillEnterForeground()的代码
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
applicationDidFinishLaunching()
这是程序开始运行时调用的函数,下面是Hello World程序中实现applicationDidFinishLaunching()的代码
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
我们知道导演是Cocos2dx中执行一切的开始,所以该函数首先定义了一个导演。
然后导演调用的前三个成员都是用来设置FPS,所以我们能在Hello World的左下角中看到FPS
接着该函数定义了一个场景(pScene),该场景等于HelloWorldScene中的场景,然后用导演运行这个场景,所以接下来我们就转到HelloWorldScene类中去看看这个场景是怎么实现的
HelloWorldScene类的代码解析
scene()
这是前面在AppDelegate类中applicationDidFinishLaunching()函数中CCScene *pScene = HelloWorld::scene();中的scene,下面我们看看它的代码
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
该场景的函数中定义了一个图层,这个图层被加在场景中,所以我们只需要对图层进行操作就可以了
init()
顾名思义这是初始化的函数
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" p_w_picpath, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *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));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
return true;
}
在这个函数中,我们看到了首先添加了一个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)
这个成员函数是但按下关闭按钮时调用的,所以他应该实现关闭程序的功能
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
—————————————————————————————————————————————
cocos2dx中各层次调用关系:
Director(导演)->Scene(场景)->Layer(图层)->Sprite(精灵)
导演只有一个,导演可以调用多个场景,场景中可以有多个图层,图层中也可以有多个精灵,图层中也可以有多个子图层,精灵可以不经过图层直接加在场景中。
转载于:https://blog.51cto.com/panjunbing/1566026