原文链接:http://blog.sina.com.cn/s/blog_7d1531ed010167pq.html
CCControlButton 是cocos2d-x新封装的UI 类似oc的UIButton 可以设置各个状态的png支持CCScale9Sprite根据大小拉伸图片!
CCSprite* tmp = CCSprite::create("CloseNormal.png");
CCSize size =tmp->getContentSize();
CCLog("wh:%f,%f", size.width, size.height);
tmp->release();
CCScale9Sprite* backGround = CCScale9Sprite::create("CloseNormal.png");
CCControlButton*button=CCControlButton::create(backGround);
button->setPosition(200, 200);
button->setPreferredSize(CCSizeMake(size.width, size.height));
this->addChild(button);
button->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::menu1Callback), CCControlEventTouchUpInside);
voidHelloWorld::menu1Callback(cocos2d::CCNode *pSender)
{
CCControlButton*button=(CCControlButton*)pSender;
CCLog("%d",button->getTag());//根据tag值判断按钮
CCLOG("menu1Callback");
}
如果button->setPreferredSize(CCSizeMake(size.width*3,size.height*3));就会变成第二张图片走形了
疑惑的是 CCScale9Sprite有什么作用是为了适应button的拉扯 效果还是那么难看!
解决办法:对CCScale9Sprite进行缩放规划才可以
CCSprite* tmp= CCSprite::create("CloseNormal.png");
CCSize size =tmp->getContentSize();
CCRectfullRect = CCRectMake(0,0,size.width, size.height);
CCRect insetRect = CCRectMake(2,2,size.width-4, size.height-4);
CCScale9Sprite* backGround = CCScale9Sprite::create("CloseNormal.png", fullRect,insetRect);