GUI动态创建button

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
	
	
	UIButton *button = UIButton::create();
	button->setTouchEnabled(true);
	button->loadTextures("close1.png", "close2.png",""); //按钮纹理
	button->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
	button->addTouchEventListener(this, toucheventselector(HelloWorld::touchEvent));//按钮的事件

	m_pUiLayer = UILayer::create(); //创建一个UI层
	m_pUiLayer->addWidget(button);//加入画布
	this->addChild(m_pUiLayer);
	
	
	 void HelloWorld::touchEvent(CCObject *pSender, TouchEventType type)
{
	CCLOG("test...");
}

原文地址:https://www.cnblogs.com/mfrbuaa/p/3881306.html