第一個例子—HelloWorld
在vs2010中啓動HelloCpp,看到如下窗口
暫時不知道什麼原因,圖片大小與窗口並不匹配,點擊右下角按鈕可以退出。在Classes目錄下有5個文件,在AppMaCros.h中定義了在不同平臺下資源的大小。
類AppDelegate繼承了CCApplication,定義了三個方法:
virtualbool applicationDidFinishLaunching();//響應窗口啓動完成後的工作
virtualvoid applicationDidEnterBackground();//響應窗口進入後臺的工作
virtualvoid applicationWillEnterForeground();//響應窗口恢復的工作。
在這個項目中,只有響應窗口啓動後的工作。 下面是其中的代碼
bool AppDelegate::applicationDidFinishLaunching() {
// initializedirector
CCDirector* pDirector =CCDirector::sharedDirector(); // 場景管理器
CCEGLView* pEGLView =CCEGLView::sharedOpenGLView(); //創建視口
pDirector->setOpenGLView(pEGLView); //設置OpenGL視口
// Set the designresolution
pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height, kResolutionNoBorder); //設置分辨率大小
CCSize frameSize =pEGLView->getFrameSize();
vector<string> searchPath; //資源路徑
//在不同的平臺下加載不同的資源
if (frameSize.height> mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height,largeResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is larger than the height of small resource size, select mediumresource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height,mediumResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is smaller than the height of medium resource size, select smallresource.
else
{
searchPath.push_back(smallResource.directory); pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height,smallResource.size.width/designResolutionSize.width));
}
//設置資源文件路徑
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn ondisplay FPS
pDirector->setDisplayStats(true); //開啓顯示FPS
// set FPS. thedefault 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); 運行HelloWorld中的場景。
return true;
}
在HelloWorld類中,定義了一個場景。
CCScene*HelloWorld::scene()
{
// 'scene' is anautorelease object
CCScene *scene = CCScene::create(); //創建場景
// 'layer' is anautorelease object
HelloWorld *layer = HelloWorld::create();//創建圖層
// add layer as achild to scene
scene->addChild(layer); //添加圖層作爲節點
// return thescene
returnscene;
}
bool HelloWorld::init()
{
//////////////////////////////
// 1. super initfirst
if (!CCLayer::init() ) //創建圖層
{
return false;
}
CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize(); //獲取大小
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin();//獲取原點
//創建關閉按鈕
CCMenuItemImage *pCloseItem =CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
//設置按鈕位置,可以看出 Cocos2d-x的座標原點是左下角
pCloseItem->setPosition(ccp(origin.x+ visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y +pCloseItem->getContentSize().height/2));
//創建菜單
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,1);
//創建 HelloWorld 文本及設置位置
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",TITLE_FONT_SIZE);
// 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 labelas a child to this layer
this->addChild(pLabel,1);
//創建精靈
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 spriteas a child to this layer
this->addChild(pSprite,0);
return true;
}