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

Cocos2d-x3.0 Button_第1张图片

你可能感兴趣的:(button,extension,cocos2d-x)