我在 cocos2d 方面有 2 年的经验,但今天开始使用 cocos2d-x。我刚刚编写了派生菜单按钮类,它不会触发任何默认方法,如 onEnter、selected 和 all。
这是我的代码:
//MyButton.h
#include <stddef.h>
#include "cocos2d.h"
using namespace cocos2d;
class MyButton : public CCMenuItemSprite
{
public:
void onEnter();
void selected();
};
//我的按钮.mm
void MyButton::onEnter()
{
CCPoint pos = this->getSelectedImage()->getPosition();
CCSize size = this->getSelectedImage()->getContentSize();
this->getSelectedImage()->setPosition(ccp(pos.x+size.width*0.5f, pos.y+size.height*0.5f));
this->getSelectedImage()->setAnchorPoint(ccp(0.5f,0.5f));
}
void MyButton::selected()
{
if (isEnabled())
{
if (this->getSelectedImage())
{
CCScaleTo *scale = CCScaleTo::create(0.5f, 1.3f);
this->getSelectedImage()->runAction(scale);
}
}
}
MyButton * MyButton::create(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
{
MyButton *btn = (MyButton*)CCMenuItemSprite::create(normalSprite, selectedSprite,target, selector);
return btn;
}
//这里是创建按钮的代码
CCSprite *start_1 = CCSprite::createWithSpriteFrameName(FRAME_MM_START);
CCSprite *start_2 = CCSprite::createWithSpriteFrameName(FRAME_MM_START);
MyButton *btnPlay =(MyButton*) MyButton::create(start_1,
start_2,
this,
menu_selector(MyMainMenu::playGame));
btnPlay->setPosition(ccp(mS.width*0.5f, mS.height*0.5f));
btnPlay->mMaxScale = 1.2f;
CCMenu* menu = CCMenu::create();
menu->setAnchorPoint( ccp(0,0) );
menu->setPosition( ccp(0,0) );
this->addChild(menu, 1);
menu->addChild(btnPlay);