先上效果图:(为啥图片显示不出来?)
在.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 !");
}创建3个MenuItemImage,再将这3个添加到menu中(最后一个NULL不可以省略掉)
下面是在按钮上添加文字,只需要将label的zorder设置的比按钮高一点就可以显示出来了
本文介绍如何在Cocos2d-x中创建并响应按钮点击事件,包括定义回调函数、创建按钮菜单及在按钮上添加文字。通过具体代码示例展示了按钮的创建过程及其交互功能。
1400

被折叠的 条评论
为什么被折叠?



