看到还是有人在问这个CCControlButton这个的如何用,刚好做项目的时候有用到,跟大家分享一下啦
当然首先用到CCControlButton必须导入外部的拓展函数extensions以及#include "cocos-ext.h"头文件名(在我的另一博文有讲过了)
在此我给大家分享我一段运用CCControlButton的代码吧
我自己把定义button的封装起来了,如下
CCControlButton * BasicLayer::getButton( const char *image,CCRect normalrect,CCRect selectedrect,CCRect deadrect )
{
CCRect rectInsets = CCRectMake(0,0,1,1);
CCScale9Sprite *pNormal= CCScale9Sprite::create(image,normalrect,rectInsets);
CCScale9Sprite *pPress = CCScale9Sprite::create(image,selectedrect,rectInsets);
CCScale9Sprite *pDead = CCScale9Sprite::create(image,deadrect,rectInsets);
CCSize size=pNormal->getContentSize();
CCControlButton *_button = CCControlButton::create(pNormal);
_button->setPreferredSize(CCSizeMake(size.width, size.height));
// m_button->setZoomOnTouchDown(false); //设置不现实按钮效果
_button->setBackgroundSpriteForState(pPress, CCControlStateHighlighted);
_button->setBackgroundSpriteForState(pDead,CCControlEventTouchUpInside);
_button->setZoomOnTouchDown(false);
return _button;
}
在此调用到调用这个函数即可,以及写回调函数如下
CCControlButton *selectbutton2=getButton("bookselect.png",CCRectMake(0,54.67*3*RATIO,115*RATIO,54.67*RATIO),CCRectMake(115*RATIO,54.67*3*RATIO,115*RATIO,54.67*RATIO),CCRectMake(115*RATIO,54.67*3*RATIO,115*RATIO,54.67*RATIO));
selectbutton2->setAnchorPoint(CCPointZero);
selectbutton2->setPosition(580*RATIO+640*number*RATIO,245*RATIO);
selectbutton2->addTargetWithActionForControlEvents(this,cccontrol_selector(SelectroleLayer::selectCallback),CCControlEventTouchDown);
selectbutton2->addTargetWithActionForControlEvents(this,cccontrol_selector(SelectroleLayer::updateCallback),CCControlEventTouchUpInside);
selectbutton2->addTargetWithActionForControlEvents(this,cccontrol_selector(SelectroleLayer::updateCallback),CCControlEventTouchDragExit);
node->addChild(selectbutton2,1,(number+1)*2+50);
再者看下回调函数的例子
void SelectroleLayer::updateCallback( CCObject *pSend,CCControlEvent event ){//在里面填写内容}