在main文件中的最后一句代码:
return CCApplication::sharedApplication()->run();
调用了CCApplication类中的run()方法开始对Windows的显示做一系列处理,将该方法中的一段代码注释掉:
if(!applicationDidFinishLaunching())
{
return 0;
}
发现窗口依然能够运行,但是却没有任何显示内容,所以断定注释掉的代码中包含显示部分的处理,进入AppDelegate类中查看该方法,发现有段生成场景类的代码:
//create a scene. it's an autorelease object
CCScene *pScene =HelloWorld::scene();
进入该方法中,此方法在 HelloWorld 类中,该类继承于 CCLayer ,在方法
CCScene* HelloWorld::scene() 中,只有四句代码:
//'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is anautorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as achild to scene
scene->addChild(layer);
// return the scene
return scene;
依次为:
1、调用 CCScene 类中的静态方法 create() 创建一个默认的场景类。
2、调用 HelloWorld 类调用静态方法 create() ,但是发现该类中没有该方法的实现,转到定义发现:
// implement the"static node()" method manually
CREATE_FUNC(HelloWorld);
所以该调用该方法时,调用的是CCNode中的某个方法,转到CCNode的源文件,找到方法
static CCNode * create(void);
virtual bool init();
在 static CCNode * create(void); 方法调用了virtual bool init(); 方法。
3、调用复写的 init() 方法
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
分别获取窗口大小和窗口起始点坐标
//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'san autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,1);
设置退出按钮各个属性
// add a label shows"Hello World"
// create andinitialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("HelloWorld","Arial", 36);
// position thelabel on the center of the screen
pLabel->setPosition(ccp
(origin.x +visibleSize.width/2,
origin.y +visibleSize.height - pLabel->getContentSize().height));
// add the label asa child to this layer
this->addChild(pLabel,1);
窗口正上方的标签显示各个属性设置
//add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position thesprite on the center of the screen
pSprite->setPosition(ccp
(visibleSize.width/2+ origin.x,
visibleSize.height/2+ origin.y));
// add the sprite asa child to this layer
this->addChild(pSprite,0);
窗口背景显示
void HelloWorld::menuCloseCallback(CCObject*pSender)
该方法为在该Layer中的Menu元素的回调事件