前端之家收集整理的这篇文章主要介绍了
cocos2d-x CCControlButton,九妹图用按钮,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<pre name="code" class="cpp">/************************************************************************/
/* CCControlButton 固定宽高的函数是:
setAdjustBackgroundImage(false)
按钮按下时改变按钮文字的颜色:
setTileColorForState(ccWHITE,CCControlStateHighlighted);
CCControlStateHighlighted表示按钮被单击处于高亮状态时改变按钮文字的颜色为前面指定的颜色;
*/
/************************************************************************/
bool bRet = false;
do
{
//这样写比较保险:
CC_BREAK_IF(!CCLayer::init());//此宏只能在循环里调用;用来代替 if () break;
//正常状态下的按钮图片;
CCScale9Sprite * btnNormal = CCScale9Sprite::create("scale9.png");
//单击状态下的图片;
CCScale9Sprite * btnDown = CCScale9Sprite::create("10.png");
//按钮标题文字;
CCLabelTTF * title = CCLabelTTF::create("Touch Me!3334444443","courier new",30);
//创建按钮,按钮的大小会根据标题自动调整;
CCControlButton * controlBtn = CCControlButton::create(title,btnNormal);
//设置按钮按下时的图片;
controlBtn->setBackgroundSpriteForState(btnDown,CCControlStateSelected);
controlBtn->setPosition(ccp(200,200));
//固定背景大小;虽然有说会自动变大,但是我用的没效果;
controlBtn->setPreferredSize(CCSize(200,200));
this->addChild(controlBtn);
//点击事件;
controlBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(CCScale::touchDownAction),CCControlEventTouchDown);
//点击事件;
/************************************************************************/
/* 当鼠标处于按下并曾经点中按钮时,则触发一次
void touchDownAction(CCObject* pSender,CCControlEvent event);
CCControlEventTouchDown
当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次
void touchDragEnter(CCObject* pSender,CCControlEvent event);
CCControlEventTouchDragEnter
当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次
void touchDragExit(CCObject* pSender,CCControlEvent event);
CCControlEventTouchDragExit
当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,只要达到条件,就不断触发
void touchDragInside(CCObject* pSender,CCControlEvent event);
CCControlEventTouchDragInside
当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,只要达到条件,就不断触发
void touchDragOutside(CCObject* pSender,CCControlEvent event);
CCControlEventTouchDragOutside
当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次
void touchUpInside(CCObject* pSender,CCControlEvent event);
CCControlEventTouchUpInside
当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次
void touchUpOutside(CCObject* pSender,CCControlEvent event);
CCControlEventTouchUpOutside
暂时没有发现能用鼠标触发这个事件的操作,看了注释,应该是由其它事件中断按钮事件而触发的
void touchCancel(CCObject* pSender,CCControlEvent event);
CCControlEventTouchCancel
*/
/************************************************************************/
bRet = true;
} while (0);
void touchDownAction(CCObject* pSender,CCControlEvent event)
{
CCSprite * spr = CCSprite::create("10.png");
addChild(spr);
spr->setPosition(ccp(200,200));
}