前端之家收集整理的这篇文章主要介绍了
自定义ToggleButton,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// 作者 : Bingzhao Chen
// 创建时间: 2016/7/1 11:38
#ifndef mToggleButton_h__
#define mToggleButton_h__
#include"cocos2d.h"
using namespace cocos2d;
class mToggleButton : public Node{
private:
MenuItemToggle* toggleItem;
public:
static mToggleButton* createWithSprite(const ccMenuCallback& callback,Sprite* normal,Sprite* selected);
inline unsigned int getSelectedIndex() const { return toggleItem->getSelectedIndex(); };
MenuItem* getSelectedItem();
private:
mToggleButton();
~mToggleButton();
virtual bool init(const ccMenuCallback& callback,Sprite* selected);
};
#endif // mToggleButton_h__
#include "mToggleButton.h"
mToggleButton::mToggleButton()
:toggleItem(nullptr)
{
}
mToggleButton::~mToggleButton()
{
}
mToggleButton* mToggleButton::createWithSprite(const ccMenuCallback& callback,Sprite* selected)
{
mToggleButton *pRet = new(std::nothrow)mToggleButton();
if (pRet && pRet->init(callback,normal,selected)){
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = nullptr;
return nullptr;
}
}
bool mToggleButton::init(const ccMenuCallback& callback,Sprite* selected)
{
if (!Node::init()){
return false;
}
auto normalItem = MenuItemSprite::create(normal,normal);
auto selectedItem = MenuItemSprite::create(selected,selected);
toggleItem = MenuItemToggle::createWithCallback(callback,normalItem,selectedItem,NULL);
toggleItem->setPosition(Vec2::ZERO);
auto menu = Menu::create(toggleItem,NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu);
return true;
}
MenuItem* mToggleButton::getSelectedItem()
{
return toggleItem->getSelectedItem();
}
原文链接:https://www.f2er.com/cocos2dx/339433.html