先上效果图:(为啥图片显示不出来?)
在.h文件中添加 void button_touch_test(Object *); 类似的这样的回调函数的声明(自己写的回调函数)
在.cpp文件中init()函数里如下写:
auto giveUpMenuItem = MenuItemImage::create( "button01.png", "button02.png", CC_CALLBACK_1(GameScene::button_giveUp, this)); giveUpMenuItem->setPosition(Point(60, 40)); auto genZhuMenuItem = MenuItemImage::create( "button01.png", "button02.png", CC_CALLBACK_1(GameScene::button_jiaZhu, this)); genZhuMenuItem->setPosition(Point(190, 40)); auto yiLvMenuItem = MenuItemImage::create( "button01.png", "button02.png", CC_CALLBACK_1(GameScene::button_yilv, this)); yiLvMenuItem->setPosition(Point(320, 40)); auto starMenu = Menu::create(giveUpMenuItem, genZhuMenuItem,yiLvMenuItem,NULL); starMenu->setPosition(Point::ZERO); this->addChild(starMenu, 1); //按钮上添加文字(暂时的功能,后期完善可以删掉) auto button_label01 = Label::createWithSystemFont("giveUp", "Arial", 17); button_label01->setPosition(Point(60, 40)); this->addChild(button_label01,2);回调函数的实现:
void GameScene::button_touch_test(Object * pSender){ CCLOG("button touched !"); }
下面是在按钮上添加文字,只需要将label的zorder设置的比按钮高一点就可以显示出来了