【Cocos遊戲實戰】功夫小子第五課之幫助場景和選關功能的實現

功夫小子之幫助場景和選關功能的實現

轉載請註明出處:http://blog.csdn.net/suool/article/details/46661231
本節課的視頻教程地址是:
第五課在此

如果本教程有幫助到您,希望您能點擊進去觀看一下,而且現在註冊成爲極客學院的會員,即日起至7月1日,極客學院的 VIP 課程 30 天免費體驗,想學編程的小夥伴速來,只針對新用戶:http://e.jikexueyuan.com/invite/index.html?ZnJvbV9jb2RlPVkxblJUZSZ1bmFtZT1TdW9vTCZjaGFubmVsPWludml0ZV8xMDB3X3NoYXJlYnV0dG9uX2RpcmVjdDE= 而且驗證手機號碼和郵箱號碼會贈送三天的會員時間,手機端首次也可以領取五天的會員時間哦(即使是購買年會員目前也僅僅是年費260),成爲極客學院學習會員可以無限制的下載和觀看所有的學院網站的視頻,謝謝您的支持!

本節課主要內容是兩項:

  • 遊戲幫助功能場景的實現
  • 遊戲選關功能場景的實現

首先是第一部分:

遊戲幫助功能場景的實現

幫助功能場景是前面課程主開始菜單場景的一個附屬場景,首先看下其效果圖:
幫助關於效果圖
通過觀察本效果圖可以看出,這個界面的重難點在於實現這個滾動中文字幕公告效果。

中文亂碼問題解決

所以,我們首先需要解決的問題是中文顯示亂碼的問題,其次是實現滾動的字幕效果。
關於解決中文亂碼問題,這個已經有一堆的文章介紹,我這裏就簡單的提一下。解決方法有三種:

  • 源碼文件保存爲utf-8格式
  • 編寫編碼轉換代碼,手動轉換中文
  • 讀取配置文件

關於第一種方法:可以部分解決該問題,但是顯示依然有問題,結尾字符顯示有可能亂碼,而且不能解析轉義字符。不推薦第一種,但是將源碼文件保存爲UTF-8的格式確實推薦,因爲VS的調試的時候編碼GBK會出現一些異常情況,關於轉換格式請參考我的CSDN的自動轉換文章:http://blog.csdn.net/suool/article/details/44604219
utf-8轉換工具

第二個手動編寫轉碼代碼,這個需要用到第三方庫,這裏我使用的是在Windows平臺下已經提供的Iconv編碼處理庫。通過在項目中添加對應的轉換工具類即可,使用該庫需要添加如下包含目錄:
包含目錄
代碼如下:
IconvString.h

/*!
 * \file IconvString.h
 * \date 2015/05/30 9:05
 *
 * \author SuooL
 * Contact: [email protected]
 *
 * \brief 字符編碼
 *
 * TODO: long description
 *
 * \note
*/

#ifndef __ICONVSTRING_H__
#define __ICONVSTRING_H__

int convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen);

int gbk2utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen);

#endif

cpp實現文件:

#include <string>
#include "IconvString.h"
#include "iconv.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
// 編譯鏈接的時候指定靜態庫
#pragma comment(lib,"libiconv.lib")
#endif

int convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
    iconv_t iconvH;
    iconvH = iconv_open(to_charset, from_charset);
    if (!iconvH) return NULL;
    memset(outbuf, 0, outlen);

#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    const char *temp = inbuf;
    const char **pin = &temp;
    char **pout = &outbuf;
    if (!iconv(iconvH, pin, &inlen, pout, &outlen))
    {
        iconv_close(iconvH);
        return NULL;
    }
#else
    if (!iconv(iconvH, &inbuf, &inlen, &outbuf, &outlen))
    {
        iconv_close(iconvH);
        return NULL;
    }
#endif
    iconv_close(iconvH);
    return NULL;
}

int gbk2utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
    return convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}

使用的方法就是:
比如在圖籍界面中有中文的Lable標籤,那麼其實現方式如下:

// 怪物圖鑑
char *inBuf = "木\n樁\n怪";
size_t inLen = strlen(inBuf);
size_t outLen = inLen << 1;
char *outBuf = (char *)malloc(outLen);
gbk2utf8(inBuf, inLen, outBuf, outLen);
······ // 使用在此
free(outBuf); // 記得釋放內存

這個實現的結果是很不錯的,但是不合適的地方在於,移植遊戲的時候在Android平臺沒有內置這個Iconv庫,所以就需要我們手動下載該庫的源碼,然後編譯,這個過程比較複雜,導致我們的工作量增加,所以不建議使用。
對於第三種方式則是最爲推薦的方式。
對於讀取配置文件的方式來說實現了編碼和界面的分離,使得國際化更爲方便。這裏我以讀取XML文件爲例講講如何使用這個方法。
首先我們需要一個XML的解析類,這裏我使用的XML格式是Android編程中的XML格式如下所示:
xml格式
因此需要解析的是name標籤及其內容。
以下是解析的代碼:
XmlParser.h

/*!
 * \file XmlParser.h
 * \date 2015/05/30 11:21
 *
 * \author SuooL
 * Contact: [email protected]
 *
 * \brief 
 *
 * TODO: long description
 *
 * \note
*/
#ifndef __XMLParser_H__
#define __XMLParser_H__

#pragma once

#include <string>
#include "cocos2d.h"

class XMLParser : public cocos2d::Ref, public cocos2d::SAXDelegator
{
public:
    // 解析指定的xml文件
    static XMLParser* parseWithFile(const char *xmlFileName);

    static XMLParser* parseWithString(const char *content);

    XMLParser();
    virtual ~XMLParser();

    // 從本地xml文件讀取
    bool initWithFile(const char *xmlFileName);
    // 從字符中讀取,可用於讀取網絡中的xml數據
    bool initWithString(const char *content);

    // 對應xml標籤開始,如:<string name="app_name">
    virtual void startElement(void *ctx, const char *name, const char **atts);

    // 對應xml標籤結束,如:</string>
    virtual void endElement(void *ctx, const char *name);

    // 對應xml標籤文本
    virtual void textHandler(void *ctx, const char *s, int len);

    // 獲取對應標籤的字符串
    cocos2d::String* getString(const char *key);

private:
    cocos2d::CCDictionary *m_pDictionary;
    std::string m_key;

    std::string startXMLElement;
    std::string endXMLElement;

};

#endif

以下是實現代碼:XMLParser.cpp

/*!
 * \class XMLParser.cpp
 *
 * \ingroup GroupName
 *
 * \brief 
 *
 * TODO: long description
 *
 * \note 
 *
 * \author SuooL
 *
 * \version 1.0
 *
 * \date 五月 2015
 *
 * Contact: [email protected]
 *
 */

#include "XMLParser.h"
#include "cocos2d.h"

using namespace std;
using namespace cocos2d;

//字符ascii碼
// 空格
const static int SPACE = 32;
// 換行
const static int NEXTLINE = 10;
// tab 橫向製表符
const static int TAB = 9;

string replace(string source, string pattern, string dstPattern)
{
    string result;
    string::size_type pos;

    int lenSource = source.length();
    int i = 0;

    for (i = 0; i < lenSource; ++i)
    {
        pos = source.find(pattern, i);
        if (string::npos == pos)
            break;

        if (pos < lenSource)
        {
            string s = source.substr(i, pos - i);
            result += s;
            result += dstPattern;
            i = pos + pattern.length() - 1;
        }
    }
    result += source.substr(i);
    return result;
}

XMLParser* XMLParser::parseWithFile(const char *xmlFileName)
{
    XMLParser *pXMLParser = new XMLParser();
    if (pXMLParser->initWithFile(xmlFileName))
    {
        pXMLParser->autorelease();
        return pXMLParser;
    }
    CC_SAFE_DELETE(pXMLParser);
    return NULL;
}

bool XMLParser::initWithFile(const char *xmlFileName)
{
    m_pDictionary = new CCDictionary();
    SAXParser _parser;
    _parser.setDelegator(this);
    //獲取文件全路徑
    string fullPath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
    CCLog("xml parser full path : %s", fullPath.c_str());

    return _parser.parse(fullPath);
}

XMLParser* XMLParser::parseWithString(const char *content)
{
    XMLParser *pXMLParser = new XMLParser();
    if (pXMLParser->initWithString(content))
    {
        pXMLParser->autorelease();
        return pXMLParser;
    }
    CC_SAFE_DELETE(pXMLParser);
    return NULL;
}

bool XMLParser::initWithString(const char *content)
{
    m_pDictionary = new CCDictionary();
    SAXParser _parse;
    _parse.setDelegator(this);
    return _parse.parse(content, strlen(content));
}

//開始一個節點
// 比如 <string name="muzhuang">木\n樁\n怪</string>
//name    爲     :string 
//atts[0] 爲屬性   : name
//atts[1] 爲值        : app_name
//atts[2] 以此類推
void XMLParser::startElement(void *ctx, const char *name, const char **atts)
{
    this->startXMLElement = (char *)name;
    CCLog("start=%s", startXMLElement.c_str());//name

    if (this->startXMLElement == "string")
    {
        while (atts && *atts)
        {
            CCLog("attrs0=%s", atts[0]);    //atts[0] : name
            CCLog("attrs1=%s", atts[1]);    //atts[1] : app_name

            const char *attsKey = *atts;
            if (0 == strcmp(attsKey, "name"))
            {
                ++atts;
                const char *attsValue = *atts;
                m_key = attsValue;          //key
                break;
            }
            ++atts;
        }

    }

}

void XMLParser::endElement(void *ctx, const char *name)
{
    this->endXMLElement = (char *)name;
    CCLog("end=%s", endXMLElement.c_str());
}

void XMLParser::textHandler(void *ctx, const char *s, int len)
{
    string value((char *)s, 0, len);

    //是否全是非正常字符
    bool noValue = true;
    for (int i = 0; i < len; ++i)
    {
        if (s[i] != SPACE && s[i] != NEXTLINE && s[i] != TAB)
        {
            noValue = false;
            break;
        }
    }
    if (noValue) return;
    string result = replace(value, string("\\n"), string("\n"));
    CCString *pString = CCString::create(result);
    CCLog("key=%s value=%s", m_key.c_str(), pString->getCString());
    this->m_pDictionary->setObject(pString, this->m_key);
}

String* XMLParser::getString(const char *key)
{
    string strKey(key);
    return (String *)this->m_pDictionary->objectForKey(strKey);
}

XMLParser::XMLParser()
{
}

XMLParser::~XMLParser()
{
    CC_SAFE_DELETE(this->m_pDictionary);
}

具體的使用方式如下(依然是圖籍界面的示例):

XMLParser *pXmlParser = XMLParser::parseWithFile("tujiLayer.xml");
String *mz = pXmlParser->getString("muzhuang");
m_pMZLabel = LabelTTF::create(mz->getCString(), "", 30); // 注意此處的getCString()

這就完美解決了中文的顯示亂碼問題,當然解析XML配置文件Cocos有提供內置的解析工具,比如TinyXML,參考此處官網文檔:Cocos2d-x xml解析 http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/xml-parse/zh.md

字幕滾動效果的實現

下面就是如何實現滾動的中文字幕效果。這個效果可以使用Cocos2d提供的一個工具類ClippingNode裁剪節點類。關於這個類的簡單說明如下:

  • ClippingNode是利用模板遮罩來完成對Node區域裁剪的技術,主要是根據一個模板(Stencil)切割圖片的節點,生成任何形狀的節點顯示
  • 首先它是一個節點,繼承於Node,所以它可以像普通節點一樣放入Layer,Scene,Node中,其次作爲節點,它就可以用作容器,承載其他節點和精靈,即是底板,如果想要對一個節點進行裁剪,那需要給出裁剪的部分,這個裁剪區域即是模版。
    ClippingNode類
    重點的API函數,源碼顯示如下:
class CC_DLL ClippingNode : public Node
{
public:
    /** Creates and initializes a clipping node without a stencil.
    創建裁剪節點
     */
    static ClippingNode* create();

    /** Creates and initializes a clipping node with an other node as its stencil.
     The stencil node will be retained.
     創建節點的同時指定模板
     */
    static ClippingNode* create(Node *stencil);

    /** The Node to use as a stencil to do the clipping.
     The stencil node will be retained.
     This default to nil.
     */
    Node* getStencil() const;
    void setStencil(Node *stencil); // 設置模板

    /** The alpha threshold.
     The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold.
     Should be a float between 0 and 1.
     This default to 1 (so alpha test is disabled).
     */
    GLfloat getAlphaThreshold() const;
    void setAlphaThreshold(GLfloat alphaThreshold); // 設置Alpha閾值,默認是1

    /** Inverted. If this is set to true,
     the stencil is inverted, so the content is drawn where the stencil is NOT drawn.
     This default to false.
     */
    bool isInverted() const;  
    void setInverted(bool inverted); // 設置顯示區域,默認顯示模板遮罩區域
    ....詳細參見源碼

關於此節點的更多使用方法參見該文章:http://shahdza.blog.51cto.com/2410787/1561937
下面就是幫助場景的實現代碼:

/*!
 * \file HelpLayer.h
 * \date 2015/05/30 15:43
 *
 * \author SuooL
 * Contact: [email protected]
 *
 * \brief  幫助關於界面 
 *
 * TODO: long description
 *
 * \note
*/

#ifndef __HelpLayer__H__
#define __HelpLayer__H__

#include "cocos2d.h"
#include "extensions/cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

class HelpLayer : public Layer {
public:
    static Scene* createScene();
    virtual bool init();
    CREATE_FUNC(HelpLayer);

private:
    LabelTTF* text;
    void scrollback();
};

#endif

cpp文件

/*!
 * \class HelpLayer
 *
 * \ingroup GroupName
 *
 * \brief  幫助關於界面 
 *
 * TODO: long description
 *
 * \note 
 *
 * \author SuooL
 *
 * \version 1.0
 *
 * \date 五月 2015
 *
 * Contact: [email protected]
 *
 */

#include "HelpLayer.h"
#include "StartLayer.h"
#include "GlobalDefine.h"
#include "cocos2d.h"
#include "XMLParser.h"
#include "extensions/cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

Scene* HelpLayer::createScene()
{
    Scene* scene = Scene::create();
    HelpLayer* helpLayer = HelpLayer::create();

    scene->addChild(helpLayer);

    return scene;
}

bool HelpLayer::init()
{
    if (!Layer::init())
    {
        return false;
    }
    auto bgSprite = Sprite::create("Help.png");
    bgSprite->setPosition(WINSIZE.width / 2, WINSIZE.height / 2);
    this->addChild(bgSprite);

    auto closeItem = MenuItemSprite::create(
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("GalleryOffNormal.png")),
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("GalleryOffSelected.png")),
        [&](Ref * ref){
        PLAYEFFECT;
        //彈出場景
        Director::getInstance()->replaceScene(StartLayer::createScene()); });
    // 關閉按鈕
    closeItem->setPosition(WINSIZE.width / 2 + 580, WINSIZE.height / 2 + 320);
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu);

    auto titleSp = Sprite::create("TitleLogo.png");
    titleSp->setPosition(WINSIZE.width / 2 + 200, WINSIZE.height / 2);
    this->addChild(titleSp);

    // 創建文字Label
    XMLParser *pXmlParser = XMLParser::parseWithFile("tujiLayer.xml");
    String *label = pXmlParser->getString("label_help");
    text = LabelTTF::create(label->getCString(), "", 23);
    text->setColor(ccc3(0, 255, 255));
    text->setTag(15);
    text->setPosition(250, 100);

    // 繪製裁剪區域
    DrawNode* area = DrawNode::create();
    Point point[4] = { Point(50, 100), Point(500, 100), Point(500, 450), Point(50, 450) };
    area->drawPolygon(point, 4, ccc4f(255, 255, 255, 255), 0, ccc4f(255, 255, 255, 255));

    // 創建遮罩層
    ClippingNode* m_pClipArea = ClippingNode::create();
    m_pClipArea->setInverted(false);
    m_pClipArea->setStencil(area);
    m_pClipArea->addChild(text);
    this->addChild(m_pClipArea);

    MoveBy* moveact = CCMoveBy::create(5.0f, Point(0, 400)); //10秒向上移動400 
    CallFunc* callFunc = CallFunc::create(this, callfunc_selector(HelpLayer::scrollback));
    // 創建連續動作
    ActionInterval* attackact = Sequence::create(moveact, callFunc, NULL);

    text->runAction(RepeatForever::create(attackact));


    return true;
}

void HelpLayer::scrollback()
{
    text->setPosition(250, 100);
}

選關功能場景的實現

這一部分的實現主要使用了一個自定義的選關控件,實現了這樣的效果:
實現效果
這個控件的實現效果我參考了這篇博文作者的思路,http://blog.csdn.net/ccy0815ccy/article/details/43924895
具體的使用方法和實現思路可以參考他的代碼,我這裏做了一點改動。這個界面重點就是這個效果控件的使用,下面是我的代碼:

/*!
 * \file GateMapLayer.h
 * \date 2015/05/31 20:09
 *
 * \author SuooL
 * Contact: [email protected]
 *
 * \brief 選關功能場景
 *
 * TODO: long description
 *
 * \note
*/

#ifndef __GateMapLayer__H__
#define __GateMapLayer__H__

#include "cocos2d.h"
USING_NS_CC;

class SelectGate;

class GateMapLayer : cocos2d::Layer
{
public:
    static Scene* createScene();
    virtual bool init();
    CREATE_FUNC(GateMapLayer);

    Sprite *bgPic;
    Sprite *closeBtn;
    Sprite *tips;
    LabelTTF * coin;
    LabelTTF * energy;

    SelectGate* selectGateMenu; // 當前處於中間的被選的關卡

    void gateOneCallBack(Ref* pSender);
    void gateTwoCallBack(Ref* pSender);
    void gateThreeCallBack(Ref* pSender);

    void closeFunc(Ref* pSender);
    void challengeFunc(Ref* pSender);

};
#endif

cpp文件如下:

/*!
* \class GateMapLayer
*
* \ingroup GroupName
*
* \brief  選關功能場景
*
* TODO: long description
*
* \note
*
* \author SuooL
*
* \version 1.0
*
* \date 五月 2015
*
* Contact: [email protected]
*
*/

#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "GateMapLayer.h"
#include "GlobalDefine.h"
#include "GlobalData.h"
#include "StartLayer.h"
#include "SelectGate.h"
#include "GameLayer.h"
#include "HelloWorldScene.h"

USING_NS_CC;
using namespace CocosDenshion;

Scene* GateMapLayer::createScene()
{
    auto scene = Scene::create();
    auto layer = GateMapLayer::create();
    scene->addChild(layer);
    return scene;
}

bool GateMapLayer::init()
{
    if (! Layer::init())
    {
        return false;
    }

    if (getBoolFromXML(MUSIC_KEY))
    {
        float music = getFloatFromXML(MUSICVOL)*100.0f;
        aduioEngine->setBackgroundMusicVolume(getFloatFromXML(MUSICVOL));
        if (SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())
        {
            aduioEngine->pauseBackgroundMusic();
            aduioEngine->playBackgroundMusic("Sound/gateMap.wav", true);
        }
        else
            aduioEngine->playBackgroundMusic("Sound/gateMap.wav", true);
    }
    else
        aduioEngine->pauseBackgroundMusic();

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("pnglist/gateMap.plist");

    bgPic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("GateMapBG.png"));
    bgPic->setPosition(WINSIZE.width / 2.0, WINSIZE.height / 2);
    this->addChild(bgPic);

    auto closeItem = MenuItemSprite::create(
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("CloseNormal.png")),
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("CloseSelected.png")),
        CC_CALLBACK_1(GateMapLayer::closeFunc, this)); // 退出

    auto challengeItem = MenuItemSprite::create(
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("tiaozhanNormal.png")),
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("tiaozhanSelected.png")),
        CC_CALLBACK_1(GateMapLayer::challengeFunc, this)); // 挑戰

    closeItem->setPosition(WINSIZE.width - 47, WINSIZE.height - 44);
    challengeItem->setPosition(WINSIZE.width - 253, 7 * WINSIZE.height / 72);
    auto menu = Menu::create(closeItem, challengeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu);

    auto gateOneItem = MenuItemSprite::create(
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Gate_1.png")),
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Gate_1.png")),
        CC_CALLBACK_1(GateMapLayer::gateOneCallBack, this)); // 退出

    auto gateTwoItem = MenuItemSprite::create(
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Gate_2.png")),
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Gate_2.png")),
        CC_CALLBACK_1(GateMapLayer::gateTwoCallBack, this)); // 退出

    auto gateThreeItem = MenuItemSprite::create(
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Gate_3.png")),
        Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Gate_3.png")),
        CC_CALLBACK_1(GateMapLayer::gateThreeCallBack, this)); // 退出

    selectGateMenu = SelectGate::create();
    selectGateMenu->addMenuItem(gateOneItem);
    selectGateMenu->addMenuItem(gateTwoItem);
    selectGateMenu->addMenuItem(gateThreeItem);
    selectGateMenu->setPosition(visibleSize.width / 2, visibleSize.height / 2 + 74);
    this->addChild(selectGateMenu, 2);

    auto m_pMoneyLabel = LabelTTF::create("1000", "", 45);
    m_pMoneyLabel->setColor(ccc3(0, 255, 255));
    m_pMoneyLabel->setPosition(300, 60);

    auto m_pEnergyLabel = LabelTTF::create("10", "", 45);
    m_pEnergyLabel->setColor(ccc3(0, 255, 255));
    m_pEnergyLabel->setPosition(640, 60);

    this->addChild(m_pMoneyLabel);
    this->addChild(m_pEnergyLabel);

    return true;
}

// a selector callback  
void GateMapLayer::gateOneCallBack(cocos2d::Ref* pSender)
{
    PLAYEFFECT;
    setIntToXML(GAMELEVEL_KEY, 1);
    setIntToXML(SELECTGATE, 1);
    UserDefault::getInstance()->flush();
    m_iSelectGate = 1;
    log("First gate select!");
    Director::getInstance()->replaceScene(GameLayer::createScene());
}
void GateMapLayer::gateTwoCallBack(cocos2d::Ref* pSender)
{
    PLAYEFFECT;
    log("Second gate select!");
    if (getBoolFromXML(GATEONE, false))
    {
        setIntToXML(GAMELEVEL_KEY, 2);
        setIntToXML(SELECTGATE, 2);
        UserDefault::getInstance()->flush();
        m_iSelectGate = 2;
        Director::getInstance()->replaceScene(HelloWorld::createScene());

    }
}
void GateMapLayer::gateThreeCallBack(cocos2d::Ref* pSender)
{
    PLAYEFFECT;
    log("Third gate select!");
    if (getBoolFromXML(GATETWO, false))
    {
        setIntToXML(GAMELEVEL_KEY, 3);
        setIntToXML(SELECTGATE, 3);
        UserDefault::getInstance()->flush();
        m_iSelectGate = 3;
        Director::getInstance()->replaceScene(HelloWorld::createScene());
    }
    else
    {
        //Label * label = Label::cr("");
    }
}

void GateMapLayer::closeFunc(Ref* pSender)
{
    PLAYEFFECT;
    Director::getInstance()->replaceScene(StartLayer::createScene());
}
void GateMapLayer::challengeFunc(Ref* pSender)
{
    PLAYEFFECT;
    selectGateMenu->getCurrentItem()->activate(); // 手動激活該項目
//  m_iSelectGate = 1;
//  Director::getInstance()->replaceScene(GameLayer::createScene());
}

這就是本節課的主要內容。


結束語

轉載請註明出處:http://blog.csdn.net/suool/article/details/46661231
在本課程中我們學習了cocos2d-x項目的幫助功能場景和選關功能場景的實現。你應當掌握了以下內容:

  • 中文亂碼問題的解決方法
  • 裁剪節點ClippingNode的原理和用法
  • 自定義控件的創建和使用過程

下節課我們將學習遊戲項目中的核心功能場景的分析與實現的分析與實現 。
轉載請註明出處:http://blog.csdn.net/suool/article/details/46661231

本節課的視頻教程地址是:
第五課在此

如果本教程有幫助到您,希望您能點擊進去觀看一下,而且現在註冊成爲極客學院的會員,即日起至7月1日,極客學院的 VIP 課程 30 天免費體驗,想學編程的小夥伴速來,只針對新用戶:http://e.jikexueyuan.com/invite/index.html?ZnJvbV9jb2RlPVkxblJUZSZ1bmFtZT1TdW9vTCZjaGFubmVsPWludml0ZV8xMDB3X3NoYXJlYnV0dG9uX2RpcmVjdDE= 而且驗證手機號碼和郵箱號碼會贈送三天的會員時間,手機端首次也可以領取五天的會員時間哦(即使是購買年會員目前也僅僅是年費260),成爲極客學院學習會員可以無限制的下載和觀看所有的學院網站的視頻,謝謝您的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章