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;
     }



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。