cocos中的自定义按钮

//文字按钮
//	auto pText=LabelTTF::create("BACK", "Arial", 36);	
	auto pText = ui::Text::create("BACK", "marryde.ttf", 40);
//	auto pText = Label::createWithSystemFont("Back", "Arial", 36);
//	pText->setPosition(Vec2(200, 200));	//设置字的位置
	pText->setColor(Color3B::RED);
	auto pMenu = Menu::create();
	pMenu->setPosition(Vec2( origin.x+pText->getContentSize().width/2, visibleSize.height + origin.y-pText->getContentSize().height/2));
	auto pMenuLabel = MenuItemLabel::create(pText, CC_CALLBACK_1(HelloWorld::menuCloseCallback2, this));
	pMenu->addChild(pMenuLabel,1);
	layer->addChild(pMenu, 3);

	//创建图片按钮
	//方法一:
	auto pNormalSprite = Sprite::create("back.png");	//导入两张图片作为精灵,代表按钮正常和选定的样子
	auto pSelectedSprite = Sprite::create("back.png");
	pNormalSprite->setScale(0.5f);//图片太大了,缩放一半
	pSelectedSprite->setScale(0.5f);
	pSelectedSprite->setColor(Color3B::GREEN);	//按钮按下的话,为了对比出区别,让它渲染绿色
	auto backItem = MenuItemSprite::create(pNormalSprite, pSelectedSprite, nullptr, CC_CALLBACK_1(HelloWorld::menuCloseCallback2, this));
	//方法二:直接一句话,因为方法二是继承的方法一
	//auto backItem = MenuItemImage::create("back.png","back.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback2, this));
	backItem->setPosition(Vec2(visibleSize.width + origin.x, visibleSize.height  + origin.y));
	auto menu = Menu::create(backItem, NULL);//菜单项放到菜单里
	menu->setPosition(Vec2::ZERO);
	layer->addChild(menu, 1);

	/* 切换按钮
	auto sprite_tab1 = Sprite::create("tab1.png");
	auto sprite_tab2 = Sprite::create("tab2.png");
	auto tab1Item = MenuItemSprite::create(sprite_tab1,nullptr);
	auto tab2Item = MenuItemSprite::create(sprite_tab2, nullptr);
	m_menutab1 = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::menuCloseCallback3, this), tab1Item, tab2Item,nullptr);
	m_menutab1->setSelectedIndex(1);
	auto sprite_tab3 = Sprite::create("tab3.png");
	auto sprite_tab4 = Sprite::create("tab4.png");
	auto tab3Item = MenuItemSprite::create(sprite_tab3, nullptr);
	auto tab4Item = MenuItemSprite::create(sprite_tab4, nullptr);
	m_menutab2 = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::menuCloseCallback3, this), tab3Item, tab4Item,nullptr);
	m_menutab2->setSelectedIndex(0);

	auto pMenuToggle = Menu::create(m_menutab1, m_menutab2, nullptr);//菜单
	pMenuToggle->setPosition(Vec2(400, 400));
	layer->addChild(pMenuToggle, 1);
	pMenuToggle->alignItemsHorizontallyWithPadding(10);	//设置水平间距
	//创建两个层,用于配合MenuItemToggle
	auto layer1 = Layer::create();
	auto plabeltemp = Label::create("TAB1 ", "Arial", 30);
	plabeltemp->setColor(Color3B::BLUE);
	layer1->addChild(plabeltemp);
	auto layer2 = Layer::create();
	plabeltemp->setColor(Color3B::BLUE);
	layer2->addChild(plabeltemp);	//两个层over
	m_pLayerMutiple = LayerMultiplex::create(layer1, layer2, nullptr);//创建LayerMultiplex
	m_pLayerMutiple->setPosition(Vec2(400, 400));
	layer->addChild(m_pLayerMutiple);*/

你可能感兴趣的:(cocos中的自定义按钮)