Cocos2d-x3.0 Button-阿里云开发者社区

开发者社区> 开发与运维> 正文

Cocos2d-x3.0 Button

简介:

Size widgetSize = Director::getInstance()->getWinSize();
        
        Text* alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 );
        alert->setColor(Color3B(159, 168, 176));
        alert->setPosition(Point(widgetSize.width / 2.0f,
                                 widgetSize.height / 2.0f - alert->getSize().height * 3.075f));
        
        addChild(alert);
        
        
        Layout* layout = Layout::create();
        layout->setSize(Size(widgetSize.width, widgetSize.height));
       
        //横向排列,这里相似Android里的线性布局
        layout->setLayoutType(LAYOUT_RELATIVE);
        /*以图片为背景*/
        layout->setBackGroundImageScale9Enabled(true);
        layout->setBackGroundImage("green_edit.png");
        
        layout->setPosition(Point(0,0));
        addChild(layout);

        
        Button* button_TopLeft = Button::create("animationbuttonnormal.png", "animationbuttonpressed.png");
        //开启后出现点击效果
        button_TopLeft->setPressedActionEnabled(true);
        layout->addChild(button_TopLeft);
        
        RelativeLayoutParameter* rp_TopLeft = RelativeLayoutParameter::create();
        rp_TopLeft->setAlign(RELATIVE_ALIGN_PARENT_TOP_LEFT);
        button_TopLeft->setLayoutParameter(rp_TopLeft);
        
        
        //top center horizontal
        Button* button_TopCenter = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
        layout->addChild(button_TopCenter);
        /*开启后能够设置Button大小*/
        button_TopCenter->setScale9Enabled(true);
        button_TopCenter->setSize(Size(150, 70));
        
        RelativeLayoutParameter* rp_TopCenter = RelativeLayoutParameter::create();
        rp_TopCenter->setAlign(RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL);
        button_TopCenter->setLayoutParameter(rp_TopCenter);
        
        Button* button_TopRight = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
        layout->addChild(button_TopRight);
        button_TopRight->addTouchEventListener(this, toucheventselector(LayoutTest::touchEvent));
        
        
        RelativeLayoutParameter* rp_TopRight = RelativeLayoutParameter::create();
        rp_TopRight->setAlign(RELATIVE_ALIGN_PARENT_TOP_RIGHT);
        button_TopRight->setLayoutParameter(rp_TopRight);
        
        
        //left center
        Button* button_LeftCenter = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
        layout->addChild(button_LeftCenter);
        
        RelativeLayoutParameter* rp_LeftCenter = RelativeLayoutParameter::create();  
        rp_LeftCenter->setAlign(RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL);  
        button_LeftCenter->setLayoutParameter(rp_LeftCenter);
        button_LeftCenter->setPressedActionEnabled(true);

void LayoutTest::touchEvent(cocos2d::Ref *pSender, TouchEventType type)
{
    switch (type) {
        case cocos2d::ui::TOUCH_EVENT_BEGAN:
            log("TOUCH_EVENT_BEGAN");
            break;
            
            
            
        case cocos2d::ui::TOUCH_EVENT_MOVED:
            log("TOUCH_EVENT_MOVED");
            break;
            
        case cocos2d::ui::TOUCH_EVENT_ENDED:
            log("TOUCH_EVENT_ENDED");
            break;
        case cocos2d::ui::TOUCH_EVENT_CANCELED:
            log("TOUCH_EVENT_CANCELED");
            break;
            
        default:
            break;
    }
}









本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5077892.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章