环境:window7 vs2012 cocos2d-x-2.2.6
代码:
执行
发现点击的时候,没有回调 touchDownAction 这些事件,求大神帮忙看一下。
代码:
void ControlButton::CreateButton(const char* name_png,const char* button_title,unsigned int num)
{
//得到按钮图片的大小
CCScale9Sprite* btn = CCScale9Sprite::create(name_png);
CCLOG("%f",btn->getContentSize().width);
CCLOG("%f",btn->getContentSize().height);
int png_height=static_cast<int>(btn->getContentSize().height);
int png_width=static_cast<int>( btn->getContentSize().width);
btn->release();
//要显示的图片大小
CCRect rect = CCRectMake(0,0,png_width,png_height); //图片的大小
CCRect rectInsets = CCRectMake(1,1,1,1); //left,right,width,height
//按钮标题,Marker Felt为字体类型,png_height为字体高度
CCLabelTTF *title = CCLabelTTF::create("", "Marker Felt",png_height-10);
//title->setOpacity(num);//设置可见度
//正常状态下的按钮图片
CCScale9Sprite *btnNormal = CCScale9Sprite::create(name_png,rect,rectInsets);
//创建按钮
controlBtn = CCControlButton::create(title,btnNormal);
//绑定事件
//controlBtn->setBright(true);
controlBtn->setEnabled(true);
controlBtn->setTouchEnabled(true);
BindButtonEven();
this->addChild(controlBtn);
}
void ControlButton::BindButtonEven()
{
if(!this->controlBtn)
return;
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDownAction),CCControlEventTouchDown);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragEnter),CCControlEventTouchDragEnter);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragExit),CCControlEventTouchDragExit);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragInside),CCControlEventTouchDragInside);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragOutside),CCControlEventTouchDragOutside);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchUpInside),CCControlEventTouchUpInside);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchUpOutside),CCControlEventTouchUpOutside);
this->controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchCancel),CCControlEventTouchCancel);
}
/* 当鼠标处于按下并曾经点中按钮时,则触发一次 */
void ControlButton::touchDownAction(CCObject* pSender, CCControlEvent event)
{
isTouch=true;
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 */
void ControlButton::touchDragEnter(CCObject* pSender, CCControlEvent event)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 */
void ControlButton::touchDragExit(CCObject* pSender, CCControlEvent event)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,只要达到条件,就不断触发 */
void ControlButton::touchDragInside(CCObject* pSender, CCControlEvent event)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,只要达到条件,就不断触发 */
void ControlButton::touchDragOutside(CCObject* pSender, CCControlEvent event)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次 */
void ControlButton::touchUpInside(CCObject* pSender, CCControlEvent event)
{
isTouch=false;
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 */
void ControlButton::touchUpOutside(CCObject* pSender, CCControlEvent event)
{
}
/* 暂时没有发现能用鼠标触发这个事件的操作,看了注释,应该是由其它事件中断按钮事件而触发的 */
void ControlButton::touchCancel(CCObject* pSender, CCControlEvent event)
{
}
执行
btn=ControlButton::create();
btn->CreateButton("bt.png");
btn->setPosition(ccp(100,100));
this->addChild(btn,2);
发现点击的时候,没有回调 touchDownAction 这些事件,求大神帮忙看一下。