on "init" you need to initialize your instance
bool HelloWorld::init()
4 //////////////////////////////
1. super init first
if ( !
CCLayer::init() )
10
11 CCSize visibleSize = CCDirector::sharedDirector()->
getVisibleSize();
12 CCPoint origin = CCDirector::sharedDirector()->
getVisibleOrigin();
14 /////////////////////////////
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
19 CCMenuItemImage *pCloseItem =
CCMenuItemImage::create(
20 CloseNormal.png",128); line-height:1.5!important">21 CloseSelected.png22 this,128); line-height:1.5!important">23 menu_selector(HelloWorld::menuCloseCallback));
24 pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/
2,128); line-height:1.5!important">25 origin.y + pCloseItem->getContentSize().height/
2));
create menu,it's an autorelease object
27 CCMenu* pMenu =
CCMenu::create(pCloseItem,128); line-height:1.5!important">28 pMenu->
setPosition(CCPointZero);
this->addChild(pMenu,128); line-height:1.5!important">31
3. add your codes below...
33
34 add a label shows "Hello World"
create and initialize a label
36 CCLabelTTF* pLabel = CCLabelTTF::create(
Hello WorldArial24);
37 position the label on the center of the screen
38 pLabel->setPosition(ccp(origin.x + visibleSize.width/
39 origin.y + visibleSize.height - pLabel->
getContentSize().height));
40 add the label as a child to this layer
this->addChild(pLabel,128); line-height:1.5!important">43
add "HelloWorld" splash screen"
44 CCSprite* pSprite = CCSprite::create(
HelloWorld.png45 position the sprite on the center of the screen
46 pSprite->setPosition(ccp(visibleSize.width/
2 + origin.x,visibleSize.height/
2 +
origin.y));
add the sprite as a child to this layer
48 this->addChild(pSprite,128); line-height:1.5!important">49
enable standard touch
51 this->setTouchEnabled(
52
54 }
不要看这个函数很长,实际上主要完成了4个事情
1)调用父类的初始化函数(CCLayer::init())完成对继承自父类成员的初始化
2)创建一个菜单(CCMenu)并在其中添加一个菜单项(CCMenuItem),将菜单显示在HelloWorld中。点击这个菜单项可以关闭程序
3)创建一个标签(CCLabel),让它在HelloWorld中显示“HelloWorld”字串
4)创建一个精灵(CCSprite),让它在HelloWorld中显示图片“HelloWorld.png”
HelloWorld::init函数中所创建的都是我们在运行起来的界面中所能看到的东西,这其中涉及到一些类将在后面学习,这里不深究。我比较好奇的是菜单项所实现的功能:点击后关闭程序(不信你亲自点一下试试)。这是怎么实现的呢?仔细分析一下菜单项的创建过程
2 CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
3 4 5 6 menu_selector(HelloWorld::menuCloseCallback));
最后一个参数形式很特别啊~ 我们先看看menu_selector是神马东西
#define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR)
又是一个宏!它所完成的事情是将_SELECTOR取址并强制转换为SEL_MenuHandler类型。那么SEL_MenuHandler又是什么类型呢?
1 typedef void (CCObject::*SEL_MenuHandler)(CCObject*);
它是一个函数指针,这类函数有一个CCObject*类型的参数。此时我们可以将创建菜单项的代码展开,看看其真实的原貌
6 (SEL_MenuHandler)&HelloWorld::menuCloseCallback);
HelloWorld::menuCloseCallback函数以回调函数的方式传入菜单项,在点击菜单项时被触发。也就是说实现关闭程序功能的是HelloWorld::menuCloseCallback函数
void HelloWorld::menuCloseCallback(CCObject*
pSender)
3 CCDirector::sharedDirector()->
end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
6 exit(
7 #endif
8 }