bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
// pDirector->enableRetinaDisplay(true);
// 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);
CCScene * pScene = CCScene::create();
CCLayer * pLayer = new TestController();
pLayer->autorelease();
pScene->addChild(pLayer);
pDirector->runWithScene(pScene);
return true;
}
AppDelegate是tests程序入口。看上面的代码,程序加载流程如下:
构造导演,导演设置相关属性(FPS,openGLView);
创建场景;
创建布景;
将布景添加到场景;
导演执行场景;
整个程序的初始工作就完成了,后面就是各个场景的内容。
运行tests工程,可以看到第一个画面是一个list,里面N多项。
第一条ActionsTest,看一下加载流程:
在AppDelegate中有一句CCLayer * pLayer = new TestController();
看TestController的构造函数
TestController::TestController()
: m_tBeginPos(CCPointZero)
{
// add close menu
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );
CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);
CCSize s = CCDirector::sharedDirector()->getWinSize();
pMenu->setPosition( CCPointZero );
pCloseItem->setPosition(CCPointMake( s.width - 30, s.height - 30));
// add menu items for tests
m_pItemMenu = CCMenu::create();
for (int i = 0; i < TESTS_COUNT; ++i)
{
CCLabelTTF* label = CCLabelTTF::create(g_aTestNames[i].c_str(), "Arial", 24);
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback));
m_pItemMenu->addChild(pMenuItem, i + 10000);
pMenuItem->setPosition( CCPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));
}
m_pItemMenu->setContentSize(CCSizeMake(s.width, (TESTS_COUNT + 1) * (LINE_SPACE)));
m_pItemMenu->setPosition(s_tCurPos);
addChild(m_pItemMenu);
setTouchEnabled(true);
addChild(pMenu, 1);
}
代码很好懂,添加关闭菜单,添加tests的item,开启Touch模式。
Touch一个item会进入对应的test,是由一个回调函数实现:
menu_selector(TestController::menuCallback)
具体可以看这个函数的实现。
然后就进入各个item场景了