【cocos2dx】添加简单的按钮 MenuItemImage

前端之家收集整理的这篇文章主要介绍了【cocos2dx】添加简单的按钮 MenuItemImage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


先上效果图:(为啥图片显示不出来?)



在.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",CC_CALLBACK_1(GameScene::button_jiaZhu,this));
	genZhuMenuItem->setPosition(Point(190,40));

	auto yiLvMenuItem = MenuItemImage::create(
		"button01.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设置的比按钮高一点就可以显示出来了

原文链接:https://www.f2er.com/cocos2dx/343066.html

猜你在找的Cocos2d-x相关文章