cocos2dx与lua学习笔记之控件学习(一)

function BAGUI:showLabel()
    local ttfConfig = { }
    ttfConfig.fontFilePath = "simhei.ttf"
    ttfConfig.fontSize = 24
    ttfConfig.outlineSize = 0

    local nameLbl = cc.Label:create()
    nameLbl:setString("test label")
    nameLbl:setTTFConfig(ttfConfig)
    nameLbl:setColor(DisplayObjectUtil.convertHexToRGB("#f8f400"))
    nameLbl:setAnchorPoint(0, 0)
    nameLbl:setPosition(0, 0)
    view:addChild(nameLbl)
end

function BAGUI:showButton()
    local btn = ccui.Button:create()
    btn:loadTextureNormal("normal.png", ccui.TextureResType.plistType)
    btn:loadTexturePressed("pressed.png", ccui.TextureResType.plistType)
    btn:loadTextureDisabled("disabled.png", ccui.TextureResType.plistType)
    btn:setTitleFontName("simhei.ttf")
    btn:setTitleText("开始游戏")
    btn:setTitleColor(cc.c4b(255, 255, 255, 1))
    btn:setTitleFontSize(24)
    btn:setName("startBtn")
    btn:setTag(862)
    btn:setTouchEnabled(true)
    btn:setPosition(500, 400)
    btn:setScale(1.0, 1.0)
    btn:setAnchorPoint(cc.p(0.5, 0.5))
    btn:setContentSize(119.0, 50.0)
    view:addChild(btn)
    UIManager:widgetTouchExtent(btn, {
        callBack = function()
            UIManager:addUI(cc.Director:getInstance():getRunningScene(), Config_UI.CALLBOARDEDITOR.name)
        end
    } )
end

function BAGUI:showText()
    local text = ccui.Text:create()
    text:setFontName("simhei.ttf")
    text:setFontSize(24)
    text:setString("提示文本")
    text:setText("需要显示哦的文本内容")
    text:setPosition(300, 240)
    text:setAnchorPoint(cc.p(0.5, 0.5))
    text:setEnabled(true)
    text:setVisible(true)
    text:setRotation(0)
    text:setName("msgTxt")
    view:addChild(text)
end

function BAGUI:showImage()
    local img = ccui.ImageView:create("logo.png", ccui.TextureResType.plistType)
    img:setScale(2, 1)
    img:setAnchorPoint(cc.p(0.5, 0.5))
    img:setPosition(12, 12)
    img:setLocalZOrder(-1)
    img:setName("LOGO")
    img:setColor(cc.c4b(128, 128, 128, 1))
    img:setContentSize(100, 100)
    img:setTouchEnabled(true)
    img:setTag(2)
    view:addChild(img)
    img:addClickEventListener( function()
        print("image click")
    end )

    local img2 = ccui.ImageView:create()
    img2:loadTexture("img2.png", ccui.TextureResType.localType)
    view:addChild(img2)
end

function BAGUI:showScrollView()
    local scrollView = ccui.ScrollView:create()
    scrollView:setClippingEnabled(true)
    scrollView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)
    scrollView:setBackGroundColorOpacity(102)
    scrollView:setBackGroundImageScale9Enabled(true)
    scrollView:setName("scrollView")
    scrollView:setTag(119)
    scrollView:setTouchEnabled(true)
    scrollView:setPosition(134, 90)
    scrollView:setAnchorPoint(cc.p(0.5, 0.5))
    scrollView:setColor(cc.c4b(255, 255, 255, 255))
    scrollView:setContentSize(cc.size(100, 100))
    -- 设置显示出来的区域大小“视口”
    scrollView:setInnerContainerSize(cc.size(400, 400))
    -- 设置内部容器的大小,决定了拖动的区域大小;它必须大于或等于setContentSize()。
    scrollView:setBoundEnabled(true)
    -- 是否开启拖动到头后的反弹效果
    scrollView:setLayoutType(ccui.LayoutType.VERTICAL)
    -- 垂直布局;ScrollView本身就是一种Layout

    --    scrollView:stopAutoScroll()
    --    scrollView:stopOverallScroll()
    --    scrollView:scrollToBottom()

    for i = 0, 10 do
        local btn = ccui.Button:create()
        btn:setName("btn" .. i)
        scrollView:addChild(btn)
    end

    local size1 = { }
    size1.width = 100
    size1.height = 500
    scrollView:setInnerContainerSize(size1)
    self.view:addChild(scrollView)

    scrollView:addEventListener( function(sender, type)
        if type == ccui.ScrollviewEventType.scrollToTop then
            print("scroll to top")
        elseif type == ccui.ScrollviewEventType.scrollToBottom then
            print("scroll to bottom")
        elseif type == ccui.ScrollviewEventType.scrollToLeft then
            print("scroll to left")
        elseif type == ccui.ScrollviewEventType.scrollToRight then
            print("scroll to right")
        else
            print("scroll to where ?")
        end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值