码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x3.0 Button

时间:2014-05-01 21:53:45      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   extension   button   setpressedactionenab   addtoucheventlistene   


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

bubuko.com,布布扣

Cocos2d-x3.0 Button,布布扣,bubuko.com

Cocos2d-x3.0 Button

标签:cocos2d-x   extension   button   setpressedactionenab   addtoucheventlistene   

原文地址:http://blog.csdn.net/qqmcy/article/details/24796677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!