c++subclasscocos2d-xccmenuitem

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));

Solution

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

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