寒風的Cocos2dx之旅之添加按钮

时间:2022-04-18 23:38:11

        要在Cocos2d中使用Button,首先引入头文件#include "ui/CocosGUI.h"  using namespace uil;

       引入完毕后,就创建button。代码如下:

void btnGoodsItem(cocos2d::Ref* pSender,Widget::TouchEventType event);

void HelloWorld::btnGoodsItem(cocos2d::Ref* pSender,Widget::TouchEventType event)
{
if (event == Widget::TouchEventType::ENDED)
{
CCLOG("touch ended");
auto scene03=firstscene::createScene();
Director::getInstance()->replaceScene(scene03);
}
}

   

               auto btnBuy = Button::create("CloseNormal.png","CloseSelected.png","CloseNormal.png");
btnBuy->addTouchEventListener(CC_CALLBACK_2(HelloWorld::btnGoodsItem, this));
btnBuy->setPosition(cocos2d::Vec2(97.39,29.34));
this->addChild(btnBuy);
btnBuy->setPressedActionEnabled(true);

     这样一个Button就做好了。功能要比MenuItem要强大的多。他可以使按钮放大等功能。