入口函数AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
//初始化游戏引擎控制器CCDirector,以便启动游戏引擎,单例对象
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// turn on display FPS
//打开fps
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
//设置绘制间隔为1/60每秒
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
//创建helloworld场景
CCScene *pScene = HelloWorld::scene();
// run
//启动引擎
pDirector->runWithScene(pScene);
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
//当应用程序将要进入后台时,调用
CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
//当应用程序回到前台时调用
CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
HelloWorld.cpp函数
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方法来吧创建的层加到场景之中
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
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" 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
//创建菜单项的图像
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);
// 将菜单加入HelloWorld层中
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
// 创建字体对象Hello World
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
// 将字体加入HelloWorld层中
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
//创建HelloWorld.png精灵对象
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
// 将HelloWorld.png精灵对象位于正中央
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
// 将HelloWorld.png精灵对象加入HelloWorld层中
this->addChild(pSprite, 0);
return true;
}
//回调函数,关闭场景
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
}