您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x学习笔记(一)HelloCpp

2013-03-07 12:40 369 查看


main.cpp是win32平台的入口类,AppDelegate是应用真正的入口:



applicationDidFinishLaunching函数就是处理导演类和场景开始及资源的适配:

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);

CCSize frameSize = pEGLView->getFrameSize();

// In this demo, we select resource according to the frame's height.
// If the resource size is different from design resolution size, you need to set contentScaleFactor.
// We use the ratio of resource's height to the height of design resolution,
// this can make sure that the resource's height could fit for the height of design resolution.

// if the frame's height is larger than the height of medium resource size, select large resource.
if (frameSize.height > mediumResource.size.height)
{
CCFileUtils::sharedFileUtils()->setResourceDirectory(largeResource.directory);
pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);
}
// if the frame's height is larger than the height of small resource size, select medium resource.
else if (frameSize.height > smallResource.size.height)
{
CCFileUtils::sharedFileUtils()->setResourceDirectory(mediumResource.directory);
pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
}
// if the frame's height is smaller than the height of medium resource size, select small resource.
else
{
CCFileUtils::sharedFileUtils()->setResourceDirectory(smallResource.directory);
pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height);
}

// 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;
}


当程序进入后台时(不活跃)会调用applicationDidEnterBackground()函数,比如在玩游戏的时候一个电话打进来,程序就会进入后台

在这里可以释放一些资源,如:停止音乐播放,停止动画等

void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}


当程序在次活跃时,会调用applicationWillEnterForeground()函数,可以在这里恢复音乐的播放,动画等

// 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();
}


HelloWorldScene场景类:

class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};


关键的函数是init,主要是构建场景中的各种元素:如菜单项(CCMenuItem)、精灵(CCSprite)、文本(CCMenuLabel)等,每次创建元素后必须使用addChild()函数将其加入到场景中,addChild()有三个参数:1、要加入场景的对象的指针,2、绘制层的顺序,3、Tag标记。Tag标记方便用函数,getChildByTag(tag)函数取回对象,以便使用,HelloWorld的init函数必须先调用父类的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" 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);
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", TITLE_FONT_SIZE);

// 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;
}


CREATE_FUNC(HelloWorld)是一个宏定义等同于:

static HelloWorld* create(){
HelloWorld* pRet = new HelloWorld();
if(pRet && pRet->init()){
pRet->autorelease();   //自动释放资源
}else{
delete pRet;
pRet = NULL;
}
return pRet;
}


void menuCloseCallback(CCObject* pSender); 是菜单项的一个回调函数,当点击菜单项就会调用此函数

// 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);
this->addChild(pMenu, 1);


CCScene* HelloWorld::scene(); 是将HelloWorld添加到场景中:

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;
}


刚接触cocos2d-x,如有错误请大家纠正,共同学习进步
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: