※此例是筆者之前網搜看到的一個遊戲佈局範例,因具有整體性之概念,代碼亦說明得很清楚,對初始進入遊戲開發很有參考價值。(筆者稍加整理補上一些須加強觀念,但忘了當初網搜時網址,有得知者請告知再補上!)
最終顯示結果:
事先預習概念:
在main.cpp中的代碼如下:(※類別變數之宣告若引用cocos2d-x函式庫內之類別,透週其類別之函式直接定義時則須在該變數前加上*作識別,其它型式則不用)
AppDelegate app; //創建應用程式實例
CCEGLView*
eglView = CCEGLView::sharedOpenGLView(); //創建OpenGL視窗實例
eglView->setFrameSize(480,320); //設置遊戲介面大小
int ret =
CCApplication::sharedApplication()->run();
//運行應用程式
在AppDelegate這個類中,定義有三個虛擬函數:
virtual bool applicationDidFinishLaunching();
//應用程式完成載入調用這個方法
virtual void applicationDidEnterBackground();
// 應用程式進入後臺調用這個方法
virtual void applicationWillEnterForeground();
//應用程式進入前臺調用這個方法
其中
applicationDidFinishLaunching()方法中,初始化導演物件,設置FPS,運行場景。
applicationDidEnterBackground()方法中,關閉動畫,停止背景音樂。
applicationWillEnterForeground()方法中,開始動畫,繼續背景音樂。
至於HelloWorld類則作為一個佈景安排,它繼承於CCLayer類,在此類中定義有三個方法:
virtual bool init();
static cocos2d::CCScene*
scene();
void menuCloseCallback(CCObject* pSender);
其中
init()方法初始化佈景中的一些類別、功能表、字元及背景之定義
scene()方法生成一個場景
menuCloseCallback()方法回應使用者點擊關閉功能表時之呼叫函式。
開始佈局:
背景:
CCSprite *spritebg=CCSprite::create("bg.jpg"); //通過圖片創建精靈(初始化)
CCSize size=CCDirector::sharedDirector()->getWinSize();//獲取平臺之螢幕大小
spritebg->setPosition(ccp(size.width/2,size.height/2));//將精靈的錨點設為螢幕的中間
addChild(spritebg,0); //將精靈添加到最底層,作為背景
背景圖片通過精靈來實現,效果如下
圖形標題:
接著添加添加圖形標題,作法和載入背景一樣(都是圖形),不過要注意,應添加在背景的上面
CCSprite *spritetile=CCSprite::create("tile.png");
CCSize s=spritetile->getContentSize(); //取得目前物件大小,作為錨點位置安排用
spritetile->setPosition(ccp(size.width/2,size.height-s.height/2));
addChild(spritetile,1); //添加到第二層
效果如下:
圖形菜單項:
CCMenuItemImage *start=CCMenuItemImage::create("start1.png",//正常狀態的圖片
"start2.png", //按下去時的圖片
this, //函數所在的類
menu_selector(HelloWorld::startGame)); //定義當這個功能表項目被按下時之回應函數,
//該函數設置為HelloWorld類中定義之成員函數,其返回值為void
//而成員函數之參數定義為CCObject*
CCSize startSize=start->getContentSize();//獲取物件大小
start->setPosition(ccp(0,(size.height-s.height)/2));//設置該功能表項目在菜單功能表中的位置,
//若不設置,則該功能表項目的錨點與菜單的錨點相同
CCMenuItemImage *select=CCMenuItemImage::create("select1.png",
"select2.png",
this, menu_selector(HelloWorld::selectGame));
CCSize selectSize=select->getContentSize();
select->setPosition(ccp(0,((size.height-s.height)/2-startSize.height/2)/2));
CCMenu *menu=CCMenu::create(start,select,NULL); //創建菜單,並將兩個功能表項目傳入
menu->setPosition(ccp(size.width/2,0)); //設置功能表的位置,若不設置則為螢幕的正中間
addChild(menu,1);
這裡創建兩個功能表項目,並加入到菜單中。回應函數先在標頭檔中聲明,在原始檔案中實現(※重點)
void HelloWorld::startGame(CCObject* pSender)
{
CCLog("startGame");
}
void HelloWorld::selectGame(CCObject* pSender)
{
CCLog("selectGame");
}
CCLog()方法為在輸出列印字串。現在完成之效果如下:
文字菜單項:
在畫面左邊定義三個文字菜單選項,其中兩個會用到CCMenuToggle功能表,此功能表點一下會形成另外一個功能表。
CCMenuItemFont *oon=CCMenuItemFont::create("on",this,menu_selector(HelloWorld::oonGame));
//新建一個文字功能表
CCMenuItemFont *ooff=CCMenuItemFont::create("off",this,menu_selector(HelloWorld::ooffGame));
//新建第二個文字功能表
CCMenuItemToggle *toggle=CCMenuItemToggle::itemWithTarget(this, //回應函數所在的類
menu_selector(HelloWorld::toggleGame), //回應函數
oon,ooff,NULL //添加的菜單項
);
toggle->setPosition(ccp(50,0));
CCMenuItemFont *Go=CCMenuItemFont::create("GO",this,menu_selector(HelloWorld::LetsGo));
Go->setPosition(ccp(70,80));
CCMenu *lastMenu=CCMenu::create(toggle,Go,NULL);
lastMenu->setPosition(ccp(50,50));
addChild(lastMenu,2);
這個是顯示 “on”狀態
這時候顯示的是“off”。
注意:點擊CCMenuItemToggle時回應的函數是CCMenuItemToggle之回應函數。
底下為HelloWorld類中之init()全部代碼:
bool HelloWorld::init()
{
bool bRet = false; //定義一個旗標作為程序是否完成之判斷值用(初值為false)
do
{
CC_BREAK_IF(! CCLayer::init()); //判別父層之初始化方法是否已完成
//背景
CCSprite *spritebg=CCSprite::create("bg.jpg");
CCSize size=CCDirector::sharedDirector()->getWinSize();
spritebg->setPosition(ccp(size.width/2,size.height/2));
addChild(spritebg,0);
//標題
CCSprite *spritetile=CCSprite::create("tile.png");
CCSize s=spritetile->getContentSize();
spritetile->setPosition(ccp(size.width/2,size.height-s.height/2));
addChild(spritetile,1);
//菜單
CCMenuItemImage *start=CCMenuItemImage::create("start1.png",
"start2.png",
this,
menu_selector(HelloWorld::startGame));
CCSize startSize=start->getContentSize();
start->setPosition(ccp(0,(size.height-s.height)/2));
CCMenuItemImage *select=CCMenuItemImage::create("select1.png",
"select2.png",
this,
menu_selector(HelloWorld::selectGame));
CCSize selectSize=select->getContentSize();
select->setPosition(ccp(0,((size.height-s.height)/2-startSize.height/2)/2));
CCMenu *menu=CCMenu::create(start,select,NULL);
menu->setPosition(ccp(size.width/2,0));
addChild(menu,1);
CCMenuItemImage *question=CCMenuItemImage::create("wh1.png","wh2.png",
this,menu_selector(HelloWorld::questionGame));
CCSize qSize=question->getContentSize();
question->setPosition(ccp(0,qSize.height+20)); //錨點自訂
CCMenuItemImage *settings=CCMenuItemImage::create("sz1.png","sz2.png",
this,menu_selector(HelloWorld::settingGame)); //錨點由Menu決定
CCMenu *m=CCMenu::create(question,settings,NULL);
m->setPosition(ccp(qSize.width,qSize.height));
addChild(m,1);
CCMenuItemFont *oon=CCMenuItemFont::create("on",this,menu_selector(HelloWorld::oonGame));
CCMenuItemFont *ooff=CCMenuItemFont::create("off",this,menu_selector(HelloWorld::ooffGame));
CCMenuItemToggle *toggle=CCMenuItemToggle::itemWithTarget(this,
menu_selector(HelloWorld::toggleGame),
oon,ooff,NULL
);
toggle->setPosition(ccp(50,0));
CCMenuItemFont *Go=CCMenuItemFont::create("GO",this,menu_selector(HelloWorld::LetsGo));
Go->setPosition(ccp(70,80));
CCMenu *lastMenu=CCMenu::create(toggle,Go,NULL);
lastMenu->setPosition(ccp(50,50));
addChild(lastMenu,2);
bRet = true; //當完成既有程序時將旗標宣告為true
} while (0);
return bRet; //將旗標回傳
}
下面是所有回應函數之實作:
void HelloWorld::startGame(CCObject* pSender)
{
CCLog("startGame");
}
void HelloWorld::selectGame(CCObject* pSender)
{
CCLog("selectGame");
}
void HelloWorld::settingGame(CCObject* pSender)
{
CCLog("settingGame");
}
void HelloWorld::questionGame(CCObject* pSender)
{
CCLog("questionGame");
}
void HelloWorld::oonGame(CCObject* pSender)
{
CCLog("oonGame");
}
void HelloWorld::ooffGame(CCObject* pSender)
{
CCLog("ooffGame");
}
void HelloWorld::toggleGame(CCObject* pSender)
{
CCLog("toggleGame");
}
void HelloWorld::LetsGo(CCObject* pSender)
{
CCLog("LetsGo");
}
※後文:剛開始看官方示例源碼時沒有頭緒一團亂(有看沒有懂),上網爬文看了幾篇文章及視頻教學才弄懂整個主體架構,當啃官網文件時找不到方向(對初學者很硬,給有點技術背景的開發員才能進入狀況),建議Google網搜一些過來人之post文才能進入核心領域,尤其是視頻更為重要。






0 意見:
張貼留言