简体   繁体   中英

Cocos2d-x Subclassing Issue CCMenuItemImage

I'm trying to create a button that is derived from the base class CCMenuItemImage. I want this button to be able to call it's function when it's first touched instead of after the touch ends. However, trying to subclass, I get an error saying it's an invald conversion.

button.ccp:

#include "button.h"

void Button::selected(){
CCLOG("SELECTED");
}

void Button::unselected(){
CCLOG("UNSELECTED");
}

Button* Button::create(const char *normalImage, const char *selectedImage, const char     *disabledImage, CCObject* target, SEL_MenuHandler selector) {
Button *button = new Button();
    if (button && button->initWithNormalImage(normalImage, selectedImage,     disabledImage, NULL, NULL))
    {
        button->autorelease();
        return button;
    }
    CC_SAFE_DELETE(button);
    return NULL;

}

button.h:

#ifndef BUTTON_H
#define BUTTON_H
#include "cocos2d.h"

class Button : public cocos2d::CCMenuItemImage{
public:
virtual void selected();

virtual void unselected();

};

#endif

SinglePlayer.ccp piece:

Button *left1 = Button::create("turncircle.png","turncircle.png", this, menu_selector(SinglePlayer::turning));

MenuItem select() is triggered on touch finished by default.

You need to subclass CCSprite with Touch registered with dispatcher and overwrite the ccTouchBegan

What I can understand is that you are trying to do manual control with the touches of your CCMenuItemImage. Actually all the touches are being handled in CCMenu not in MenuItem so you have to inherit CCMenu rather CCMenuItemImage to override the touches.

In my game I had this problem with CCTableView and CCMenuItem where MenuItem was a prioritised in taking gestures. So I tweaked it with inheriting CCMenu.

It also contain some extra code but just to make everything gets intact I am pasting everything.

ScrollMenu.h Class

class ScrollMenu:public CCMenu
{
public:
ScrollMenu();
virtual ~ScrollMenu(){};

bool isMovedGesture_;
bool istabBar_;
CCMenuItem * previousSelectedItem_;
static ScrollMenu* create(CCMenuItem* item,...);
virtual void registerWithTouchDispatcher();

virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch *touch, CCEvent* event);
CREATE_FUNC(ScrollMenu);
};

class ScrollMenuLoader : public cocos2d::extension::CCNodeLoader
{
public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(ScrollMenuLoader, loader);

protected:
    CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(ScrollMenu);
};

ScrollMenu.cpp Class

#include "ScrollMenu.h"

ScrollMenu* ScrollMenu::create(CCMenuItem* item, ...)
{
    va_list args;
    va_start(args, item);
    ScrollMenu *pRet = new ScrollMenu();
    if (pRet && pRet->initWithItems(item,args))
    {
        pRet->autorelease();
        va_end(args);
        return pRet;
    }
    va_end(args);
    CC_SAFE_DELETE(pRet);
    return NULL;
}
ScrollMenu::ScrollMenu()
{
    isMovedGesture_ = false;
}

void ScrollMenu::registerWithTouchDispatcher()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}

bool ScrollMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
    return CCMenu::ccTouchBegan(touch, event);
}

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

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

    CCMenuItem * currentItem = this->itemForTouch(touch);
    if(!currentItem && isMovedGesture_ && m_pSelectedItem)
    {

        if(!istabBar_ || (previousSelectedItem_ && previousSelectedItem_ != m_pSelectedItem))
        {
            m_pSelectedItem->unselected();
        }
    }
    else if(currentItem)
    {
        if(currentItem == m_pSelectedItem)
        {
            if(!isMovedGesture_)
            {
                m_pSelectedItem->activate();
                previousSelectedItem_ = m_pSelectedItem;
            }
            else{
                if(previousSelectedItem_ != m_pSelectedItem)
                {
                    m_pSelectedItem->unselected();
                }
            }

        }
        else
        {
            if(isMovedGesture_)
            {
                m_pSelectedItem->unselected();
                m_pSelectedItem = currentItem;
                m_pSelectedItem->activate();
                previousSelectedItem_ = m_pSelectedItem;
            }

        }
        if (!istabBar_) {
            currentItem->unselected();
        }
    }

    m_eState = kCCMenuStateWaiting;
    isMovedGesture_ = false;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM