2012-12-18 46 views
0

我試圖創建一個從基類CCMenuItemImage派生的按鈕。我希望這個按鈕能夠在第一次觸摸時調用它的功能,而不是在觸摸結束後調用它。然而,試圖子類化,我得到一個錯誤,說這是一個無效轉換。Cocos2d-x子類問題CCMenuItemImage

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片:

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

你定義這個創建.h文件中()函數?如果沒有,你應該把'Button * Button :: create(const char * normalImage,const char * selectedImage,const char * disabledImage,CCObject * target,SEL_MenuHandler selector);'在那裏。 –

+0

@ m.ding它說'SEL_MenuHandler'尚未聲明。 –

+1

是否在'h文件中放置了'using namespace cocos2d;'? –

回答

2

菜單項選擇()被觸發觸摸通過完成默認。

你需要繼承CCSprite與調度員註冊Touch和覆蓋ccTouchBegan

1

我能理解的是,你正在試圖做手工通過觸摸CCMenuItemImage控制。實際上所有的觸摸都在CCMenu中處理,而不是在MenuItem中,所以你必須繼承CCMenu而不是CCMenuItemImage來覆蓋觸摸。

在我的遊戲中,我有CCTableView和CCMenuItem的問題,其中MenuItem是採取手勢的優先次序。所以我調整了繼承CCMenu。

它還包含一些額外的代碼,但只是爲了使一切都完好無損我粘貼的一切。

ScrollMenu.h類

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類

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

} 
+0

任何想法如何使用cocos2d-x版本3.x完成這個任務? – user1185305