Cocos2d-x3.0 Button

本文详细介绍了在Cocos2d-x3.0中使用Button组件的方法,包括如何添加扩展功能,启用按压动作以及添加触摸事件监听器,为游戏开发提供交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值