Cocos2d-x3.3Final(3) Button常用成员函数(C++)

Button * button = Button::create("cocosui/animationbuttonnormal.png");
//新建按钮

Button* button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); //新建按钮

button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this));
//设置按钮的触摸事件和作用目标(继承自Widget)

button->setZoomScale(0.4f);

//3.3新增函数,按钮点击后会变大为原来的系数1+这里的系数0.4,

//可搭配Slider配合使用,slider->getPercent()

//float zoomScale = percent * 0.01;

//btn->setZoomScale(zoomScale);

button->setPressedActionEnabled(true);  //设置按钮是否启用点击缩放的效果

button->setOpacity(100); //继承自node,设置node透明度,0完全透明,100完全不透明

button->loadTextureNormal("cocosui/animationbuttonnormal.png");
//加载正常情况下的填充图

btn->loadTexturePressed("cocosui/animationbuttonpressed.png");
//加载按下按钮情况下的填充图

button->setScale9Enabled(true); //是否启用九宫格显示模式

button->setContentSize(Size(150, 70)); //设置按钮的大小(继承自Node)

button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
//设置按钮的位置(继承自Node)

button->runAction(Sequence::create(FadeIn::create(0.5),DelayTime::create(1.0),FadeOut::create(0.5), nullptr));
//执行一个动作或动作序列(继承自Node)

Touch事件处理函数的四种Touch状态:

Widget::TouchEventType::ENDED:

Widget::TouchEventType::CANCELED:

Widget::TouchEventType::MOVED:

Widget::TouchEventType::BEGAN:

button2->setName("normal"); //设置名字

button->setColor(Color3B::GREEN); //设置按钮的颜色

button2->setPosition(button->getPosition() + Vec2(100,0));
//(设置位置参数可以这样使用相加)

btn2->setAnchorPoint(Vec2(0,0.5)); //设置锚点

button->setTitleText("Title Button!"); //设置按钮的标题

button->setTitleColor(Color3B::YELLOW); //标题颜色

button->setFlippedX(true); //设置按钮在X方向上翻转

auto label = button->getTitleRenderer(); 
//按钮的标题渲染器是一个label

button->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(1.0f, 1.2f),

ScaleTo::create(1.0f, 1.0f),nullptr)));
//按钮一直重复膨胀1.2倍和回复原状的动作

{ //点击删除自己

Layout *layout = Layout::create();

layout->setContentSize(widgetSize * 0.6f);

layout->setBackGroundColor(Color3B::GREEN);

layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);

layout->setBackGroundColorOpacity(100);

layout->setPosition(Size(widgetSize.width/2, widgetSize.height/2));

layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE);

layout->setTag(12);

_uiLayer->addChild(layout);

Button* button = Button::create("cocosui/animationbuttonnormal.png",

"cocosui/animationbuttonpressed.png");

button->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));

//        button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));

button->addTouchEventListener(CC_CALLBACK_2(UIButtonTestRemoveSelf::touchEvent, this));

layout->addChild(button);

auto layout = _uiLayer->getChildByTag(12);

layout->removeFromParentAndCleanup(true); //删除自己删除自己,注意按钮是放在Layout中

}

button->ignoreContentAdaptWithSize(false);
//继承自Widget,设置参数为true,setContentSize失效,大小等于函数getVirtualRendererSize的返回值

{//设置按钮标题的效果

Label *title = button->getTitleRenderer();

title->enableShadow(Color4B::BLACK,Size(2,-2)); //阴影

title2->enableOutline(Color4B::GREEN, 3);
//It only works on IOS and Android when you use System fonts

}

button2->setNormalizedPosition(Vec2(0.8f, 0.5f));
//等价

void setNormalizedPosition(Vec2 pos) {

Size s = getParent()->getContentSize();

_position = pos * s;

}

时间: 2024-10-10 01:20:33

Cocos2d-x3.3Final(3) Button常用成员函数(C++)的相关文章

string 类操作的重载实现及其提供的其他常用成员函数

目录 1,string 类操作的重载实现 2,String类提供的其他常用成员函数 @ 1,string 类操作的重载实现 /* string 类操作的重载实现 */ class CMyString { public: CMyString(char *ptr = NULL) { if (ptr == NULL) { mpStr = new char[1]; *mpStr = '\0'; } else { mpStr = new char[strlen(ptr) + 1]; strcpy(mpSt

Cocos2d-x3.3Final(7)Focus常用成员函数(C++)

主要函数有 _horizontalLayout->setFocused(true);//设置焦点聚焦在该布局上 _horizontalLayout->setLoopFocus(true);//在该布局内循环设置焦点 _horizontalLayout->setFocusEnabled();//设置该控件上聚焦 _horizontalLayout->isFocusEnabled();//是否可以聚焦 这里说明几点特殊情况 VBox创建的布局,如果isLoopFocus(true);即

Cocos2d-x3.3Final(1) ListView常用成员函数(C++)

ListView listView = ListView::create(); --创建列表 listView->setDirection(ui::ScrollView::Direction::VERTICAL); --设置列表方向 listView->setBounceEnabled(true); --设置弹力 listView->setBackGroundImage("cocosui/green_edit.png"); --设置背景图片 listView->

Cocos2d-x3.3Final(5)LoadingBar常用成员函数(C++)

loadingbar就是进度条,如果要实现进度条的滑动,我们必须要实现update函数例如: void HelloWorld::update(float delta) { _count++; if (_count > 100) _count = 0; LoadingBar * loadingBar = static_cast<LoadingBar *>(getChildByTag(0)); loadingBar->setPercent(_count); //设置进度条的位置 } 除

Cocos2d-x3.3Final(6)Slider常用成员函数(C++)

滑动栏: Slider * slider = Slider::create(); slider->loadBarTexture("sliderTrack.png");//设置滑动栏的背景图片 slider->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", ""); //滑动栏的滑动按钮图片 slider->loadProgressBarTe

Cocos2d-x3.3Final(12)TextField常用成员函数(C++)

TextField * textField = TextField::create("input words here", "Arial", 30); textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent, th

Cocos2d-x3.3Final(4) ImageView常用成员函数(C++)

说明这个系列只是提供函数的熟悉和方便查找,实在没什么技术含量- - ImageView * imageView = ImageView::create("cocosui/ccicon.png");//新建图片 imageView->setScale9Enabled(true); //是否启用九宫格显示模式 imageViewChild->setSizeType(Widget::SizeType::PERCENT);//设置大小类型 imageViewChild->se

Cocos2d-x3.3Final(11)TextBMFont常用成员函数(C++)

TextBMFont:来自http://www.cocos2d-x.org/wiki/Widget的释义,In addition to UILabelAtlas, you can also display a label by UILabelBMFont. 显然这两个是都是可以显示一个标签的另外一个控件,和TextAtlas一样,但显然这两个有不同. TextBMFont * textBMFont = TextBMFont::create("BMFont",  "cocosu

Cocos2d-x3.3Final(10)TextAtlas常用成员函数(C++)

TextAtlas  //来自于官网 http://www.cocos2d-x.org/wiki/Widget的释义, UILabelAtlas can display number label that jointed by images: atlas的英文含义是地图集,所以释义可以解释为可以显示由图片组合而成的数字标签的标签地图集. TextAtlas * textAtlas = TextAtlas::create("12780", "cocosui/labelatlas