【小(xiǎo)編推薦】cocos2d 添加菜單(>δvs2012)

2014-09-06   | ♥•©λ;  發布者:梁國(guó)芳  ​∏ |   查看(kàn±₽):3320次

cocos2d
如(rú)何給 cocos2d  增加一(yī)些(xiē)菜單呢$®←(ne)。比如(rú)幫助 等按鈕。

1.在classes   右鍵  添加新建項  •§>÷;QQ截圖20140906191457


選擇頭文(wén)件(jiàn)。位置改成 項目的(de) Cla Ω<♣sses 目錄下(xià)。


頭文(wén)件(jiàn)創建好(hǎo)後。開(kāi)始創建  ≤&;cpp文(wén)件(jiàn)。  Ω¥∏$ 同上(shàng)方法一(yī)些(xiē)樣。  文¥✔£↔(wén)件(jiàn)類型選擇.cpp.   ☆↕Ω


2.創建好(hǎo)後。先放(fàng)一(♠δ±"yī)邊,一(yī)會(huì)再完善。。先在HelloWor¥±₽×ldScene.cpp中增加 菜單 。
源碼 已有(yǒu)菜單   close 
 auto closeItem = MenuItemImage$φβ ::create(
      &nb≈¥™₹sp;       &≈ nbsp;     &♦φnbsp;     & φφnbsp;       ✔  ;     &nbs≥↕₽πp;    "Cl​★®oseNormal.png",
        ↔​         $¶€      ₩"&✔          &nb♦♦ ®sp;     &nbsπ≥'p;    "CloseSπβ♠♥elected.png",
          &¶& nbsp;       ♠©α          &n‌¥π¥bsp;        ★® ;      CC_C ÷ALLBACK_1(HelloWorld::menuCloseCallbacπ±£←k, this));
    
closeItem->setPosition÷α(Point(origin.x + visibleSize.width - ←€$<closeItem->getContentSize& γ().width/2 ,
        &n₹>bsp;         &nbs∞¥☆∞p;       &nΩ≈βbsp;     or<<™€igin.y + closeItem->getConten↑‌λtSize().height/2));
我們在下(xià)面增加菜單項。

          ×↓<           &n ©bsp;
//創建新的(de)菜單 。
auto   gameItem = MenuItemFoε£nt::create("Start"≠π‌$;,CC_CALLBACK_1(HelloWorld::menuClo ←‍★seCallback,this));
auto   helpItem = MenuItemFon'<​∞t::create("Help&quo↔←t;,CC_CALLBACK_1(HelloWorld::menu∏✔↔CloseCallback,this));
    auto   aboutIt Ω‌em = MenuItemFont::create‌©♥("About",CC_CALLBAC÷×±★K_1(HelloWorld::menuCloseCallback,this)÷¥π);
 
 
gameItem->setPosition(Pointα£(origin.x + visibleSize.width/2 - clα±oseItem->getContent≤™">Size().width/2 ,300));
helpItem->setPosition(Poinλ☆‍t(origin.x + visibleS↑ ¶ize.width/2 - closeItem->getCon&₽$tentSize().width/2 ,250));"↓$
aboutItem->setPos♥≠★ition(Point(origin.x + viλ←sibleSize.width/2 - closeItem->getCΩ♠ontentSize().width/2 ,200)♠<₹™);
 
gameItem->setColor(Color3B::Y '∏ELLOW); 
helpItem->setColo∞§'₩r(Color3B::YELLOW);  
aboutItem->setColor(C±$<&olor3B::YELLOW);
 
    // creΩ✘ate menu, it's an autorelease obj±£‍♦ect
    auto menu±" = Menu::create(closeItem,aboutItem€∑>,helpItem,gameItem, NULL);


==================== ÷ ®====================β£®=




這(zhè)時(shí)點擊start  help & ¥•nbsp;about  都(dōu)會(huì)退出程序。 因為(∏±wèi)都(dōu)執行(xíng)了(le)回調§ 函數(shù) void HelloWorld::mΩ↔✘↕enuCloseCallback(Ref* pSende∑♦≤↑r)。

怎麽去(qù)區(qū)分(fēn)下(xià)。?
helpItem->setTag(12);  Ω∑→設置一(yī)個(gè)标志(zhì)。


closeItem->setTag(10);
gameItem->setTag(11);
helpItem->setTag(12);
aboutItem->setTag(13);
然後就(jiù)可(kě)以區(qū)分(fēn)回調函₽∑數(shù)了(le)。

回調函數(shù)改為(wèi)。
void HelloWorld::menuCloseCallback(' →Ref* pSender)
{
 
MenuItem  * nowItem = (MenuItem↔≈"∑ * )pSender;
 
switch(nowItem->getTag()){
case  10:
#if (CC_TARGET_PLATFORM == C∑αC_PLATFORM_WP8) || (CC_TARGET_∞®PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close ↔​button. Windows Store Apps do not imp< ✘lement a close button.",&qσ​uot;Alert");
    return;
#endif
 
    Director±‌::getInstance()->end(≠♣¶​);
 
#if (CC_TARGET_PLATFOR↔≤₩ M == CC_PLATFORM_IOS)
    exit(0);
#endif
break;
case  11:
//當 getTag  等于1λ¶∑1的(de)時(shí)候執行(xíng)的(de)語句
break;
case   12:
 
break;
 
case  13:
 
break;
}
===================================•$α=======
4.以上(shàng) closeIteεπm 函數(shù)已經完成。也(yě)就(jiù)是(shìΩ♦)  nowItem->getTag(≥¥ )   為(wèi) 10 的(de)回調函±∞☆€數(shù)。

寫  GameItem  的(™≥∑de)回調函數(shù)。也(yě)就(jiù)是ε±'♥(shì)  nowItem-&γ&gt;getTag()   為(wèi¥>) 11 的(de)回調函數(shù)。

Director::getInstance()-& ₩>φgt;replaceScene(GameSceα¥ne::createScene());  //跳(tiào)轉場(c♥↔hǎng)景   。跳(tiào)轉到(dào)  Gam♦  eScene中。

因此。還(hái)需要(yào)在GameScene.cp∑‌p  和(hé) GameScene.h中 ×₹寫代碼。

GameScene.h
#ifndef  GAME_SCENE<δ‍    //如(rú)果沒有(yǒu)定義GAME_SCENγ®∑E .就(jiù)定義。  防止®β重複定義。
#define  GAME_SCENE
#include   "cocos2d.h&quo λ™t; //引入cocos2d 
using  namespace  cocos2d;±★  //使用(yòng)cocos2d的(de)命名空(kōn←γ✘g)間(jiān)
class  GameScene : Layer{  /₹♣☆↕/定義一(yī)個(gè)遊戲的(de)場(chǎng)γ≠景類  ,繼承Layer。
public:
    bool i ✔$nit();  
 
CREATE_FUNC(GameScen↑'"e);
 
static  Scene * createScene();
 
};//注意此;    c++中有(yǒu);的(de)™₹↔"。
#endif
GameScene.cpp
#include "GameScen'♠♥e.h"
bool  GameScene::init(){
if(!Layer::init()){
return  false;
}
auto  spbk = Sprite::create("☆‍π>;background.png");
this->addChild(spbk);
return  true;
}
Scene * GameScene::createS¥φ™€cene(){
auto  scene = Scene::c ♠€φreate();
auto  layer = Gam→"eScene::create();
scene->addChild(layer); &nbs¥ ₹p; //添加圖層
return   scene;
}
然後在開(kāi)始的(de)文(wén)件(jiàn)中引入 ¥λ     #include ↓♦₩¶ "GameScene.h&↑ ‍♥quot;   。就(jiù)可(kě)以  →✔ 場(chǎng)景跳(tiào)轉了(le)。