自定的简单按钮,直接贴代码:
MenuItem* MenuItemNode::create( const char* normal,Ref* target,SEL_MenuHandler select ) { auto _normal = Sprite::create(normal); return MenuItemNode::create(_normal,target,select); } MenuItem* MenuItemNode::create( Sprite* normal, Ref* target, SEL_MenuHandler selector ) { Rect _rect = Rect(normal->getPositionX(),normal->getPositionY(),normal->getContentSize().width,normal->getContentSize().height); auto select = Sprite::createWithTexture(normal->getTexture(),_rect,false); select->setColor(ccGRAY); select->setScale(1.1f); auto _menuItem = MenuItemSprite::create(normal,select,target,selector); return _menuItem; }
找到CCMenuItem.cpp 修改方法:void MenuItemSprite::setSelectedImage(Node* image)
代码:
void MenuItemSprite::setSelectedImage(Node* image) { if (image != _normalImage) { if (image) { addChild(image, 0, kSelectedTag); //image->setAnchorPoint(Point(0, 0)); //替换 image->setAnchorPoint(Point((image->getScaleX() -1)/2 , (image->getScaleY()-1)/2)); } if (_selectedImage) { removeChild(_selectedImage, true); } _selectedImage = image; this->updateImagesVisibility(); } }
PS:图片的变色可以自已根据需要去调,修改这个方法的参数:
select->setColor(ccGRAY);//现在是灰色,可以换成其他如高亮,偏黄等自定义颜色
cocos2dx --- 按钮点击居中放大,布布扣,bubuko.com
原文地址:http://blog.csdn.net/notes_abc/article/details/32339235