欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

商店按钮【三种状态】ShopItem And ShopMenu

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接:
http://www.firedragonpzy.com.cn/index.php/archives/3919

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
【加群请写:Software MyZone或者是firedragonpzy】
淘宝店:【应小心的易淘屋】初次开店,大家多多支持……
群论坛:火龙论坛正试运营阶段,欢迎大家多提些建设性意见……

最近做了一个商店,使用菜单的三种状态,不可用【m_pDisabledImage】,可用【m_pNormalImage】,选中【m_pSelectedImage】。后因需求的变动,不可用状态【m_pDisabledImage】转为可用状态(依然是【m_pDisabledImage】),通过其它展示是否可用,可用状态【m_pNormalImage】还是可用状态,选中状态还是选中状态,不过点击离开后还是选中状态【m_pSelectedImage】,原来的是【m_pNormalImage】。

汇总一下:1)三种状态都可点击,通过其它判断是否可用。2)状态分为:不可用【其实是可用的】、可用、选中(这三种状态的描述我是根据CCMenu来说的)。因为之前用了CCMenu,为了省事,我直接改写了CCMenu和CCMenuItemSprite,这样子我就直接可以使用新的菜单和菜单项了。当然 ,你也可以直接自己写精灵重新封装,条条大路通罗马,我只是选择了其中一种,废话不多说,分享下我的ShopItem和ShopMenu,代码如下:
ShopItem.h

//
//  ShopItem.h
//  IronPig
//
//  Created by firedragonpzy on 13-9-3.
//
//




#ifndef __IronPig__ShopItem__
#define __IronPig__ShopItem__




#include "cocos2d.h"
USING_NS_CC;




class ShopItem : public CCMenuItemSprite {
public:
    /** creates a menu item with a normal,selected  and disabled image with target/selector */
    static ShopItem * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
    virtual void activate();
};




#endif /* defined(__IronPig__ShopItem__) */

 

ShopItem.cpp

//
//  ShopItem.cpp
//  IronPig
//
//  Created by firedragonpzy on 13-9-3.
//
//

#include "ShopItem.h"

ShopItem * ShopItem::create(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
{
    ShopItem *pRet = new ShopItem();
    pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
    pRet->autorelease();
    return pRet;
}

void ShopItem::activate()
{
    if (m_pListener && m_pfnSelector)
    {
        (m_pListener->*m_pfnSelector)(this);
    }
    
    if (kScriptTypeNone != m_eScriptType)
    {
        CCScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
    }
}

 

ShopMenu.h

//
//  ShopMenu.h
//  IronPig
//
//  Created by firedragonpzy on 13-9-3.
//
//




#ifndef __IronPig__ShopMenu__
#define __IronPig__ShopMenu__




#include "cocos2d.h"
USING_NS_CC;
#include "ShopItem.h"
class ShopMenu : public CCMenu
{
public:
    /** creates an empty CCMenu */
    static ShopMenu* create();
    
    /** creates a CCMenu with it's items */
    static ShopMenu* create(CCMenuItem* item, ...);
    
    /** creates a CCMenu with a CCArray of CCMenuItem objects */
    static ShopMenu* create(CCArray* pArrayOfItems);
    
    static ShopMenu* createWithItems(CCMenuItem* item, va_list args);
    
    static ShopMenu* createWithArray(CCArray* pArrayOfItems);
    
    virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
    virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
    CCMenuItem* itemForTouch(CCTouch * touch);
    virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
private:
     CCMenuItem *m_pLastItem;
};




#endif /* defined(__IronPig__ShopMenu__) */

 

ShopMenu.cpp

//
//  ShopMenu.cpp
//  IronPig
//
//  Created by firedragonpzy on 13-9-3.
//
//

#include "ShopMenu.h"

USING_NS_CC;

ShopMenu* ShopMenu::create()
{
    return ShopMenu::create(NULL, NULL);
}


ShopMenu * ShopMenu::create(CCMenuItem* item, ...)
{
    va_list args;
    va_start(args,item);
    
    ShopMenu *pRet = ShopMenu::createWithItems(item, args);
    
    va_end(args);
    
    return pRet;
}


ShopMenu* ShopMenu::createWithItems(CCMenuItem* item, va_list args)
{
    CCArray* pArray = NULL;
    if( item )
    {
        pArray = CCArray::create(item, NULL);
        CCMenuItem *i = va_arg(args, CCMenuItem*);
        while(i)
        {
            pArray->addObject(i);
            i = va_arg(args, CCMenuItem*);
        }
    }
    
    return ShopMenu::createWithArray(pArray);
}


ShopMenu* ShopMenu::createWithArray(CCArray* pArrayOfItems)
{
    ShopMenu *pRet = new ShopMenu();
    if (pRet && pRet->initWithArray(pArrayOfItems))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}


ShopMenu* ShopMenu::create(CCArray* pArrayOfItems)
{
    ShopMenu *pRet = new ShopMenu();
    if (pRet && pRet->initWithArray(pArrayOfItems))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}

void ShopMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    CC_UNUSED_PARAM(event);
    CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
}


bool ShopMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
    CC_UNUSED_PARAM(event);
    if (m_eState != kCCMenuStateWaiting || ! m_bVisible || !isEnabled())
    {
        return false;
    }
    if (!isEnabled()) {
        return true;
    }
    
    for (CCNode *c = this->m_pParent; c != NULL; c = c->getParent())
    {
        if (c->isVisible() == false)
        {
            return false;
        }
    }

    m_pSelectedItem = this->itemForTouch(touch);
    
   
    if (m_pSelectedItem)
    {
        
        m_eState = kCCMenuStateTrackingTouch;
        if (!m_pSelectedItem->isEnabled()) {
            return true;
        }
        m_pSelectedItem->selected();
        return true;
    }
    return false;
}

void ShopMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
    CC_UNUSED_PARAM(touch);
    CC_UNUSED_PARAM(event);
    m_pLastItem = m_pSelectedItem;
    CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
    ((ShopItem*)m_pSelectedItem)->activate();
    m_eState = kCCMenuStateWaiting;
}

CCMenuItem* ShopMenu::itemForTouch(CCTouch *touch)
{
    
    CCPoint touchLocation = touch->getLocation();
    
    if (m_pChildren && m_pChildren->count() > 0)
    {
        CCObject* pObject = NULL;
        CCARRAY_FOREACH(m_pChildren, pObject)
        {
            CCMenuItem* pChild = dynamic_cast<ccmenuitem *>(pObject);
            if (pChild && pChild->isVisible())
            {
                CCPoint local = pChild->convertToNodeSpace(touchLocation);
                CCRect r = pChild->rect();
                r.origin = CCPointZero;
                
                if (r.containsPoint(local))
                {
                    if ( m_pLastItem && m_pLastItem->isEnabled()) {
                        m_pLastItem->unselected();
                    }
                    return pChild;
                }
            }
        }
    }
    
    return NULL;
}


Demo下载链接:https://github.com/firedragonpzy/FD_Libs.git

发表评论