【Cocos游戏实战】功夫小子第四课之设置功能和图籍场景的实现

【Cocos游戏实战】功夫小子第四课之设置功能和图籍场景的实现,第1张

概述转载请注明出处:http://blog.csdn.net/suool/article/details/46553463 本节课的视频教程地址是: 第四课在此 如果本教程有帮助到您,希望您能点击进去观看一下,而且现在注册成为极客学院的会员,验证手机号码和邮箱号码会赠送三天的会员时间,手机端首次也可以领取五天的会员时间哦(即使是购买年会员目前也仅仅是年费260),成为极客学院学习会员可以无限制的下载和

转载请注明出处:http://www.jb51.cc/article/p-ulyytgcj-cc.html
本节课的视频教程地址是:
第四课在此

如果本教程有帮助到您,希望您能点击进去观看一下,而且现在注册成为极客学院的会员,验证手机号码和邮箱号码会赠送三天的会员时间,手机端首次也可以领取五天的会员时间哦(即使是购买年会员目前也仅仅是年费260),成为极客学院学习会员可以无限制的下载和观看所有的学院网站的视频,谢谢您的支持!

第三课我们学习了过渡场景和主开始菜单的分析和实现。这节课我们讲学习下如何实现主开始菜单场景的附属场景设置功能场景和图籍场景。

设置功能场景

首先是设置功能场景的分析和实现。
整个设置功能场景要实现的就是设置游戏的音乐和音效的设置并将设置保存下来。
所以这里我们首先需要知道是如何保存用户的数据,在我们的Cocos2d-x引擎中提供了一个类似数据存储的功能API

Userdefault类

这个类的作用和一些重要的API我们可以通过其源码很容易看出来:

/** *首先说明的是这个类的作用类似于一个小型数据库,其实是以键值对的形式存储数据的 * UserDefault acts as a tiny database. You can save and get base type values by it. * For example,setBoolForKey("played",true) will add a bool value true into the database. * Its key is "played". You can get the value of the key by getBoolForKey("played"). * * It supports the following base types:支持以下几种特殊的数据类型的存储 * bool,int,float,double,string */class CC_DLL UserDefault{public:    // 获取数据的方法,提供键取值    // get value methods    /** @brIEf Get bool value by key,if the key doesn't exist,a default value will return. You can set the default value,or it is false. * @Js NA */    bool    getBoolForKey(const char* pKey);    /** * @Js NA 如果取不到该键对应的值,则返回指定的默认值,一下的方法都是一样的 */    bool    getBoolForKey(const char* pKey,bool defaultValue)    int     getIntegerForKey(const char* pKey);    int     getIntegerForKey(const char* pKey,int defaultValue);    float    getfloatForKey(const char* pKey);    float    getfloatForKey(const char* pKey,float defaultValue);    double  getDoubleForKey(const char* pKey);    double  getDoubleForKey(const char* pKey,double defaultValue);    std::string getStringForKey(const char* pKey);    std::string getStringForKey(const char* pKey,const std::string & defaultValue);    Data getDataForKey(const char* pKey);    Data getDataForKey(const char* pKey,const Data& defaultValue);    // 存储数据的方法,指定键和值存储数据,方法和上述取值一样    // set value methods    voID    setBoolForKey(const char* pKey,bool value);    voID    setIntegerForKey(const char* pKey,int value);    voID    setfloatForKey(const char* pKey,float value);    voID    setDoubleForKey(const char* pKey,double value);    voID    setStringForKey(const char* pKey,const std::string & value);    voID    setDataForKey(const char* pKey,const Data& value);    voID    flush();    /** returns the singleton 获取user default的一个实例(此处是单例模式) * @Js NA * @lua NA */    static UserDefault* getInstance();    /** * @Js NA */    static voID destroyInstance();    // 已经弃用的方法    /** deprecated. Use getInstace() instead * @Js NA * @lua NA */    CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault();    /** * @Js NA */    CC_DEPRECATED_ATTRIBUTE static voID purgeSharedUserDefault();    /** 这个方法就说明userdefault存储的数据其实在xml文件中,这个是获取xml文件路径 * @Js NA */    const static std::string& getXMLfilePath();    /** 判断存储数据的xml文件是否存在,可以根据这个来判断是否是首次运行这个程序 * @Js NA */    static bool isXMLfileExist();    // 更多源码和实现请自行参考源码的cpp文件和文档};

然后看下设置功能场景的ui效果图:

可以看出这个功能场景的控件就三种类型,一个是菜单按钮——关闭按钮和保存按钮以及音乐和音效的开关按钮。而是精灵图片——背景图片等,三是滑动条的控件。因此此处我们需要学习的两个全新的控件就是滑动条的控件和开关按钮的控件,他们对应的Cocos2d-x的控件是:滑动条:ControlSlIDer,开关按钮MenuItemToggle。

ControlSlIDer的用法

滑动条控件的组成分为三部分——互动的背景图,滑块,划过区域的图。看下他的关键源码:

ControlSlIDer.cpp /* 继承自Control控件类,这个类有三个子类——ControlSlIDer(滑动条),Controlbutton(按钮类,这个在后面会用到),ControlSwitch(开关类)。Control类为它的子类提供了一系列的触摸响应绑定的函数。具体参考Control的源码。 */class ControlSlIDer: public Control{public:    /** 通过一个背景图片,划过区域,滑块图片名称创建一个滑动条。 * Creates slIDer with a background filename,a progress filename and a * thumb image filename. */    static ControlSlIDer* create(const char* bgfile,const char* progressfile,const char* thumbfile);    /** 通过一个背景图片精灵,划过区域精灵,滑块图片精灵创建一个滑动条 * Creates a slIDer with a given background sprite and a progress bar and a * thumb item. * * @see initWithSprites */    static ControlSlIDer* create(Sprite * backgroundSprite,Sprite* pogresssprite,Sprite* thumbSprite);    // 以上两个方法仅仅是参数不同,但是第一个其实在方法内部也是使用的精灵实现的    /** * Creates slIDer with a background filename,a progress filename,a thumb * and a selected thumb image filename. */    static ControlSlIDer* create(const char* bgfile,const char* thumbfile,const char* selectedThumbSpritefile);    /** 多了一个选中的滑块的图片,下面方法一样 * Creates a slIDer with a given background sprite and a progress bar,a thumb * and a selected thumb . * * @see initWithSprites */    static ControlSlIDer* create(Sprite * backgroundSprite,Sprite* thumbSprite,Sprite* selectedThumbSprite);    /** * @Js ctor */    ControlSlIDer();    /** * @Js NA * @lua NA */    virtual ~ControlSlIDer();    /** 初始化一个SlIDer使用参数中的精灵,各个参数的意义见下面的注释 * Initializes a slIDer with a background sprite,a progress bar and a thumb * item. * * @param backgroundSprite Sprite,that is used as a background. * @param progresssprite Sprite,that is used as a progress bar. * @param thumbSprite Sprite,that is used as a thumb. */    virtual bool initWithSprites(Sprite * backgroundSprite,Sprite* progresssprite,Sprite* thumbSprite);    /** * Initializes a slIDer with a background sprite,that is used as a thumb. * @param selectedThumbSprite Sprite,that is used as a selected thumb. */    virtual bool initWithSprites(Sprite * backgroundSprite,Sprite* selectedThumbSprite);    virtual voID needsLayout();    // 常用的API    virtual voID setMaximumValue(float val); // 设置滑动的最大值    virtual voID setEnabled(bool enabled);   // 设置能否响应    virtual bool istouchInsIDe(touch * touch);     Point locationFromtouch(touch* touch);    virtual voID setValue(float val);     // 手动设置滑动条的值    virtual voID setMinimumValue(float val); // 设置最小值    ``````    // 更多方法请自查文档和源码 };

他的父类Control提供了一个绑定事件的函数如下:

/** * Adds a target and action for a particular event to an internal dispatch * table. * The action message may optionnaly include the sender and the event as * parameters,in that order. * When you call this method,target is not retained. * * @param target The target object¡ªthat is,the object to which the action * message is sent. It cannot be nil. The target is not retained. * @param action A selector IDentifying an action message. It cannot be NulL. * @param controlEvent A control event for which the action message is sent. * See "CCControlEvent" for constants. */    voID addTargetWithActionForControlEvent(Ref* target,Handler action,EventType controlEvent);// 其中Eventype是Control的强枚举类型:这里需要使用的是VALUE_CHANGE数据变化/** Kinds of possible events for the control objects. */    enum class EventType    {        touch_DOWN           = 1 << 0,// A touch-down event in the control.        DRAG_INSIDE          = 1 << 1,// An event where a finger is dragged insIDe the bounds of the control.        DRAG_OUTSIDE         = 1 << 2,// An event where a finger is dragged just outsIDe the bounds of the control.        DRAG_ENTER           = 1 << 3,// An event where a finger is dragged into the bounds of the control.        DRAG_EXIT            = 1 << 4,// An event where a finger is dragged from within a control to outsIDe its bounds.        touch_UP_INSIDE      = 1 << 5,// A touch-up event in the control where the finger is insIDe the bounds of the control.        touch_UP_OUTSIDE     = 1 << 6,// A touch-up event in the control where the finger is outsIDe the bounds of the control.        touch_CANCEL         = 1 << 7,// A system event canceling the current touches for the control.        VALUE_CHANGED        = 1 << 8      // A touch dragging or otherwise manipulating a control,causing it to emit a serIEs of different values.    };

然后是MenuItemToggle的用法,这个控件的学习方法和上面一样,查看其源码就可以知道他的构造性方法和重要的API函数,我这里就不再带着大家走一遍了。
下面是该界面的主要实现代码:

// 头文件/*! * \file SetLayer.h * \date 2015/05/23 8:21 * * \author Suool * Contact: hu1020935219@gmail.com * * \brIEf 设置场景,用来设置音效,音乐,音量等 * * Todo: long description * * \note */#ifndef __SetLayer__H__#define __SetLayer__H__#include "cocos2d.h"#include "extensions/cocos-ext.h"USING_NS_CC;using namespace cocos2d::extension;class SetLayer : public Layer {public:    static Scene* createScene();    virtual bool init();    CREATE_FUNC(SetLayer);    voID changeMusicVol(Ref* pSender,Control::EventType type);    voID changeEffectVol(Ref* pSender,Control::EventType type);    voID saveSet(Ref* pSender);    voID closeBtn(Ref* pSender);    voID musicSet(Ref* pSender);    voID effectSet(Ref* pSender);};#endif

实现代码

Scene* SetLayer::createScene(){    Scene* setScene = Scene::create();    SetLayer* layer = SetLayer::create();    setScene->addChild(layer);    return setScene;}bool SetLayer::init(){    if (!Layer::init())    {        return false;    }    float music = getfloatFromXML(MUSICVol)*100.0f;    float effect = getfloatFromXML(SOUNDVol)*100.0f;    // 背景精灵和关闭,保存按钮初始化    auto BG = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("image-0.png"));    BG->setposition(WINSIZE.wIDth / 2,WINSIZE.height / 2);    //BG->setcolor(cocos2d::ccGRAY);    auto bgInterface = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("BGPicSet.png"));    bgInterface->setposition(WINSIZE.wIDth / 2 + 50,WINSIZE.height / 2);    auto closeBtn = MenuItemSprite::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("closeSetnormal.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("closeSetSelected.png")),CC_CALLBACK_1(SetLayer::closeBtn,this));    closeBtn->setposition(WINSIZE.wIDth - 150,WINSIZE.height - 100);    auto saveBtn = MenuItemSprite::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("SaveSettings.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("SaveSettings.png")),CC_CALLBACK_1(SetLayer::saveSet,this));    saveBtn->setposition(WINSIZE.wIDth/2+40,WINSIZE.height / 2 - 190);    // 音乐开关按钮    auto musicOn = MenuItemSprite::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("unchecked.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("unchecked.png")));    auto musicOff = MenuItemSprite::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("Hook.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("Hook.png")));    // 音效开关按钮    auto effectOn = MenuItemSprite::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("unchecked.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("unchecked.png")));    auto effectOff = MenuItemSprite::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("Hook.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("Hook.png")));    auto musicToggle = MenuItemToggle::createWithTarget(this,menu_selector(SetLayer::musicSet),musicOn,musicOff,NulL);    musicToggle->setposition(369.5,457);    auto effectToggle = MenuItemToggle::createWithTarget(this,menu_selector(SetLayer::effectSet),effectOn,effectOff,NulL);    effectToggle->setposition(369.5,357);    // 根据当前的音乐和音效状态设置开关按钮的初始化状态    if (getBoolFromXML(MUSIC_KEY))    {        musicToggle->setSelectedindex(1);    }    else    {        musicToggle->setSelectedindex(0);    }    if (getBoolFromXML(SOUND_KEY))    {        effectToggle->setSelectedindex(1);    }    else    {        effectToggle->setSelectedindex(0);    }    // 封装MenuItem到Menu并添加到场景中来    auto menu = Menu::create(closeBtn,saveBtn,musicToggle,effectToggle,NulL);    menu->setposition(Point::ZERO);    // 初始化创建滑动条控件    auto musicslIDer = ControlSlIDer::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("bgbar.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("progressbar.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("ThumbBtn.png")));    // 绑定回调函数    musicslIDer->addTargetWithActionForControlEvents(this,cccontrol_selector(SetLayer::changeMusicVol),Control::EventType::VALUE_CHANGED);    auto effectSlIDer = ControlSlIDer::create(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("bgbar.png")),Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("ThumbBtn.png")));    // 绑定回调函数    effectSlIDer->addTargetWithActionForControlEvents(this,cccontrol_selector(SetLayer::changeEffectVol),Control::EventType::VALUE_CHANGED);    // 设置滑动条控件的基本属性    musicslIDer->setposition(800,457);    musicslIDer->setMinimumValue(0.0f);    musicslIDer->setMaximumValue(100.0f);    musicslIDer->setMinimumAllowedValue(0.0f);    musicslIDer->setMaximumAllowedValue(100.0f);    musicslIDer->setValue(music);    effectSlIDer->setposition(800,357);    effectSlIDer->setMinimumValue(0.0f);    effectSlIDer->setMaximumValue(100.0f);    effectSlIDer->setMinimumAllowedValue(0.0f);    effectSlIDer->setMaximumAllowedValue(100.0f);    effectSlIDer->setValue(effect);    // 将控件添加到场景中    this->addChild(BG);    this->addChild(bgInterface);    this->addChild(musicslIDer);    this->addChild(effectSlIDer);    this->addChild(menu);    return true;}// 滑动条控件的回调函数voID SetLayer::changeMusicVol(Ref* pSender,Control::EventType type){    auto temp = (ControlSlIDer*)pSender;    setfloatToXML(MUSICVol,temp->getValue()/100.0f);    UserDefault::getInstance()->flush();    aduioEngine->setBackgroundMusicVolume(getfloatFromXML(MUSICVol));}voID SetLayer::changeEffectVol(Ref* pSender,Control::EventType type){    auto temp = (ControlSlIDer*)pSender;    setfloatToXML(SOUNDVol,temp->getValue()/100.0f);    UserDefault::getInstance()->flush();    aduioEngine->setEffectsVolume(getfloatFromXML(SOUNDVol));}// 菜单按钮的回调函数voID SetLayer::closeBtn(Ref* pSender){    PLAYEFFECT;    Director::getInstance()->replaceScene(StartLayer::createScene());}voID SetLayer::saveSet(Ref* pSender){    PLAYEFFECT;    Director::getInstance()->replaceScene(StartLayer::createScene());}// 开关按钮的回调函数voID SetLayer::musicSet(Ref* pSender){    auto musicTemp = (MenuItemToggle*)pSender;    PLAYEFFECT;    if (musicTemp->getSelectedindex() == 0)    {        setBoolToXML(MUSIC_KEY,false);        UserDefault::getInstance()->flush();        SimpleAudioEngine::getInstance()->pauseBackgroundMusic();    }    else    {        setBoolToXML(MUSIC_KEY,true);        UserDefault::getInstance()->flush();        aduioEngine->setBackgroundMusicVolume(getfloatFromXML(MUSICVol));        SimpleAudioEngine::getInstance()->resumeBackgroundMusic();    }}voID SetLayer::effectSet(Ref* pSender){    auto effectTemp = (MenuItemToggle*)pSender;    if (effectTemp->getSelectedindex() == 0)    {        setBoolToXML(SOUND_KEY,false);        UserDefault::getInstance()->flush();    }    else    {        setBoolToXML(SOUND_KEY,true);        UserDefault::getInstance()->flush();    }}

以上就是设置功能场景的基本实现,其所使用的新的内容首先是UserDefault类保存和读取用户的数据,然后是ControlSlIDer类和MenuItemToggle类。

图籍场景的实现

图籍场景的UI效果如所示:

从上面的UI效果图可以看出.这个场景中主要要实现的效果是左边的一个列表选择,中间的列表项展示区域以及一个标签提示。
关于能够实现列表显示的控件Cocos2d-x提供的相关的类有:ListVIEw,tableVIEw,以及一个相关的PageVIEw。
这三个类都有些复杂,限于时间和篇幅,这里难以展开细说提供给大家几个学习的博文和demo:
tableVIEw
http://www.jb51.cc/article/p-wdfoghun-hy.html

ScrollVIEw
http://www.jb51.cc/article/p-uwhqrcdx-wx.html

ListVIEw:
这个可以直接看我们项目中的对ListvIEw的使用。最好还是看一下源码,分析下他的实现方式,这样如果以后需要自己定制类似的控件也会方便许多。
这个场景的实现代码如下:

Scene* TujiLayer::createScene(){    Scene* scene = Scene::create();    TujiLayer* tjLayer = TujiLayer::create();    scene->addChild(tjLayer);    return scene;}bool TujiLayer::init(){    if (!Layer::init())    {        return false;    }    m_iBeforeSel = 0;    // 背景    auto bgSprite = Sprite::create("PhotogalleryBackground.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;        //d出场景        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);    bgSprite->addChild(menu);    // 怪物图鉴    m_pMZ_Pic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("ManWood.png"));    m_pMZ_Txt = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("Text.png"));    m_pMZLabel = LabelTTF::create("木\n桩\n怪","",30);    m_pMZLabel->setcolor(ccc3(0,255,255));    m_plion_Pic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("Manlion.png"));    m_plionLabel = LabelTTF::create("狮\n子\n怪",30);    m_plionLabel->setcolor(ccc3(0,255));    m_pStone_Pic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("ManStone.png"));    m_pStoneLabel = LabelTTF::create("石\n头\n怪",30);    m_pStoneLabel->setcolor(ccc3(0,255));    m_pMZ_Pic->setposition(WINSIZE.wIDth / 2 + 50,WINSIZE.height / 2);    m_pMZ_Pic->setVisible(true);    m_pMZ_Txt->setposition(WINSIZE.wIDth / 2 + 460,WINSIZE.height / 2 );    m_pMZ_Txt->setVisible(true);    m_pMZLabel->setposition(WINSIZE.wIDth / 2 + 265,WINSIZE.height / 2 - 120);    m_pMZLabel->setVisible(true);    m_plion_Pic->setposition(WINSIZE.wIDth / 2+50,WINSIZE.height / 2);    m_plion_Pic->setVisible(false);    m_plionLabel->setposition(WINSIZE.wIDth / 2 + 265,WINSIZE.height / 2 - 120);    m_plionLabel->setVisible(false);    m_pStone_Pic->setposition(WINSIZE.wIDth / 2,WINSIZE.height / 2 );    m_pStone_Pic->setVisible(false);    m_pStoneLabel->setposition(WINSIZE.wIDth / 2 + 265,WINSIZE.height / 2 - 120);    m_pStoneLabel->setVisible(false);    bgSprite->addChild(m_pMZ_Pic);    bgSprite->addChild(m_pMZ_Txt);    bgSprite->addChild(m_pMZLabel);    bgSprite->addChild(m_plion_Pic);    bgSprite->addChild(m_plionLabel);    bgSprite->addChild(m_pStone_Pic);    bgSprite->addChild(m_pStoneLabel);    ListVIEw = ListVIEw::create();    ListVIEw->setDirection(SCRolLVIEW_DIR_VERTICAL);    ListVIEw->settouchEnabled(true);    ListVIEw->setBounceEnabled(true);  // 反d    ListVIEw->setSize(Size(445,500));    ListVIEw->ignoreContentAdaptWithSize(false);    ListVIEw->setAnchorPoint(Point(0.0,0.0));    ListVIEw->setposition(Point(0,WINSIZE.height / 4));    ListVIEw->addEventListenerListVIEw(this,SEL_ListVIEwEvent(&TujiLayer::selectedItemEvent));    bgSprite->addChild(ListVIEw);    button* default_button = button::create("Cell_0.png","CellSel_0.png",UI_TEX_TYPE_PList);    default_button->setname("Title button");    Layout* default_item = Layout::create();    default_item->settouchEnabled(true);    default_item->setSize(default_button->getSize());    default_button->setposition(Point(default_item->getSize().wIDth / 2.0f,default_item->getSize().height / 2.0f));    default_item->addChild(default_button);    // set model 将创建的模板设置为项目模板     ListVIEw->setItemmodel(default_item);    // 1    button* custom_buttonMZ = button::create("CellSel_0.png","Cell_0.png",UI_TEX_TYPE_PList);    custom_buttonMZ->setname("one button");    custom_buttonMZ->setScale9Enabled(true);    custom_buttonMZ->setSize(default_button->getSize());    Layout *custom_itemmZ = Layout::create();    custom_itemmZ->setSize(custom_buttonMZ->getSize());    custom_buttonMZ->setposition(Point(custom_itemmZ->getSize().wIDth / 2.0f,custom_itemmZ->getSize().height / 2.0f));    custom_itemmZ->addChild(custom_buttonMZ);    // 2    button* custom_buttonlion = button::create("CellSel_1.png","Cell_1.png",UI_TEX_TYPE_PList);    custom_buttonlion->setname("two button");    custom_buttonlion->setScale9Enabled(true);    custom_buttonlion->setSize(default_button->getSize());    Layout *custom_itemlion = Layout::create();    custom_itemlion->setSize(custom_buttonlion->getSize());    custom_buttonlion->setposition(Point(custom_itemlion->getSize().wIDth / 2.0f,custom_itemlion->getSize().height / 2.0f));    custom_itemlion->addChild(custom_buttonlion);    // 3    button* custom_buttonStone = button::create("CellSel_2.png","Cell_2.png",UI_TEX_TYPE_PList);    custom_buttonStone->setname("three button");    custom_buttonStone->setScale9Enabled(true);    custom_buttonStone->setSize(default_button->getSize());    Layout *custom_itemStone = Layout::create();    custom_itemStone->setSize(custom_buttonStone->getSize());    custom_buttonStone->setposition(Point(custom_itemStone->getSize().wIDth / 2.0f,custom_itemStone->getSize().height / 2.0f));    custom_itemStone->addChild(custom_buttonStone);    // 4    button* custom_buttonWood = button::create("CellSel_3.png","Cell_3.png",UI_TEX_TYPE_PList);    custom_buttonWood->setname("four button");    custom_buttonWood->setScale9Enabled(true);    custom_buttonWood->setSize(default_button->getSize());    Layout *custom_itemWood = Layout::create();    custom_itemWood->setSize(custom_buttonWood->getSize());    custom_buttonWood->setposition(Point(custom_itemWood->getSize().wIDth / 2.0f,custom_itemWood->getSize().height / 2.0f));    custom_itemWood->addChild(custom_buttonWood);    ListVIEw->pushBackCustomItem(custom_itemmZ);    ListVIEw->pushBackCustomItem(custom_itemlion);    ListVIEw->pushBackCustomItem(custom_itemStone);    ListVIEw->pushBackCustomItem(custom_itemWood);    return true;}// ListVIEw触摸监听voID TujiLayer::selectedItemEvent(Ref* pSender,ListVIEwEventType  type)//ListVIEw触摸事件{    ListVIEw* ListVIEw = static_cast<ListVIEw*>(pSender);    CC_UNUSED_ParaM(ListVIEw);// if (m_iBeforeSel == ListVIEw->getCurSelectedindex())// {// return;// }// String *normalBtn = String::createWithFormat("Cell_%d.png",m_iBeforeSel);// String *SelctBtn = String::createWithFormat("CellSel_%d.png",ListVIEw->getCurSelectedindex());    m_iBeforeSel = ListVIEw->getCurSelectedindex();    switch (m_iBeforeSel)    {    case 0:        m_pMZ_Pic->setVisible(true);        m_pMZ_Txt->setVisible(true);        m_pMZLabel->setVisible(true);        m_plion_Pic->setVisible(false);        m_plionLabel->setVisible(false);        m_pStone_Pic->setVisible(false);        m_pStoneLabel->setVisible(false);        break;    case 1:        m_pMZ_Pic->setVisible(false);        m_pMZLabel->setVisible(false);        m_pMZ_Txt->setVisible(false);        m_plionLabel->setVisible(true);        m_plion_Pic->setVisible(true);        m_pStoneLabel->setVisible(false);        m_pStone_Pic->setVisible(false);        break;    case 2:        m_pMZ_Pic->setVisible(false);        m_pMZ_Txt->setVisible(false);        m_pMZLabel->setVisible(false);        m_plion_Pic->setVisible(false);        m_plionLabel->setVisible(false);        m_pStone_Pic->setVisible(true);        m_pStoneLabel->setVisible(true);        break;    case 3:        m_pMZ_Pic->setVisible(false);        m_pMZ_Txt->setVisible(false);        m_plion_Pic->setVisible(false);        m_pStone_Pic->setVisible(false);        m_plionLabel->setVisible(false);        m_pStoneLabel->setVisible(false);        m_pMZLabel->setVisible(false);        break;    default:        break;    }}

这就是我们的图籍场景的实现,所使用的是ListVIEw,这个控件的使用方法是非常简单的,但是需要我们了解下其背后的实现原理。
这里有两个问题先提出来,一个是ListVIEw 3.0版本的在AndroID平台下可能会出现白屏绿底的现象。二是我们的这个场景的标签在windows平台下面是乱码。具体的解决办法我们会在后面的给出答案,当然您也可以提前找下解决的方法。

本节课的视频教程地址是:第四课在此

如果本教程有帮助到您,希望您能点击进去观看一下,而且现在注册成为极客学院的会员,验证手机号码和邮箱号码会赠送三天的会员时间,手机端首次也可以领取五天的会员时间哦(即使是购买年会员目前也仅仅是年费260),成为极客学院学习会员可以无限制的下载和观看所有的学院网站的视频,谢谢您的支持!

转载请注明出处:http://www.jb51.cc/article/p-ulyytgcj-cc.html

总结

以上是内存溢出为你收集整理的【Cocos游戏实战】功夫小子第四课之设置功能和图籍场景的实现全部内容,希望文章能够帮你解决【Cocos游戏实战】功夫小子第四课之设置功能和图籍场景的实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061640.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存