cocos2d-lua 简单实用方法

119 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。


function util.checkInvalidChar(str,symbol)
    str=str or ""

    local tmp,tmp1,tmp2
    local i=1

    while i <= #str do
        tmp = string.byte(str,i)
        i = i + 1

        if tmp < 192 and not symbol then
            if (tmp >= 48 and tmp <= 57) or (tmp>=65 and tmp<=90) or (tmp>=97 and tmp<=122) then
            else               
                return true
            end

            --192及以上为双字节字符
            --224~239为三字节汉字
            elseif tmp >= 228 and tmp < 240 then
                --此处因为汉字一从228开始,前面是其他符号可抛弃
                tmp1=string.byte(str,i)
                i=i+1
                if tmp1>191 or tmp1<128 then
                    return true
                end
                tmp2=string.byte(str,i)
                i=i+1
                if tmp2>191 or tmp2<128 then
                    return true
            end
        --240及以上为四字节字符
        elseif tmp>=240 then
            return true
        end
    end
end

----------------------------------------scrollview--------------------------------------------------------

function UITool.ShowListEX(ScrollViewObj,Data,CallBack,DIR)
    --做个兼容处理--
    if type(Data)=="table" then
        DataCount=table.nums(Data)
        ScrollViewObj["Data"]=Data
    else
        DataCount=Data
        ScrollViewObj["Data"]={}
    end
    ScrollViewObj:setBounceEnabled(false)
    ScrollViewObj:setScrollBarEnabled(false)
    ScrollViewObj:registerScriptHandler(function(e)
        if e=="exit" then  
            ScrollViewObj["ItemMode"]:release()
            for _,V in pairs(ScrollViewObj["ShowItemObjArr"]) do
                V:release()
            end
        end
    end)

if not ScrollViewObj["ItemMode"] then
        ScrollViewObj["ItemMode"]=ScrollViewObj:getChildren()[1]
        ScrollViewObj["ItemMode"]:retain()
    end
    if DIR=="H" then
       return UITool.ShowListHorScroll(ScrollViewObj,DataCount,CallBack)
    end
    local SVHeight=ScrollViewObj:getContentSize().height
    local ItemSize=ScrollViewObj["ItemMode"]:getContentSize()
    local InnerHeight=math.max(SVHeight,ItemSize.height*DataCount)
    ScrollViewObj:setInnerContainerSize(cc.size(ItemSize.width,InnerHeight)) --改变会触发onEvent事件--
    ScrollViewObj["DataCount"]=DataCount
    ScrollViewObj:removeAllChildren()

local Y=ScrollViewObj:getInnerContainerPosition().y
    ScrollViewObj:setInnerContainerPosition(cc.p(0,math.min(0,Y)))

    local MaxItemCount=math.ceil(SVHeight/ItemSize.height)+1
    ScrollViewObj["ShowItemObjArr"]=ScrollViewObj["ShowItemObjArr"] or {}
    local ItemCount=table.nums(ScrollViewObj["ShowItemObjArr"]) --已有--
    for i=ItemCount+1,math.min(MaxItemCount,DataCount) do
        local ItemObj=ScrollViewObj["ItemMode"]:clone()
        ItemObj:setAnchorPoint(cc.p(0,0))
        ItemObj:retain()
        ItemObj:setContentSize(ItemSize)
        table.insert(ScrollViewObj["ShowItemObjArr"],ItemObj)
    end
    local PreIndex = 0

ScrollViewObj.onEventCallBack=function(e,ForceRefresh)
        local Y=ScrollViewObj:getInnerContainerPosition().y
        local ScrollSpace=InnerHeight+Y-SVHeight
        local CurIndex=math.ceil(ScrollSpace/ItemSize.height)
        CurIndex=math.max(1,CurIndex)
        if PreIndex~=CurIndex or ForceRefresh then
            PreIndex=CurIndex
            for i=1,table.nums(ScrollViewObj["ShowItemObjArr"]) do
                ScrollViewObj["ShowItemObjArr"][i]:setVisible(false)
                if CurIndex+i-1<=ScrollViewObj["DataCount"] then

ScrollViewObj["ShowItemObjArr"][i]:setVisible(true)
                    CallBack(CurIndex+i-1,ScrollViewObj["ShowItemObjArr"][i],ScrollViewObj["Data"][CurIndex+i-1])
                    ScrollViewObj["ShowItemObjArr"][i]:setPositionY(InnerHeight-ItemSize.height*(CurIndex+i-1))
                    if not ScrollViewObj["ShowItemObjArr"][i]:getParent() then
                        ScrollViewObj:addChild(ScrollViewObj["ShowItemObjArr"][i])
                    end
                end
            end
        end
    end
    ScrollViewObj:onEvent(ScrollViewObj.onEventCallBack)
    ScrollViewObj.onEventCallBack()
end

--------------------------------------------------------------------------------------------------------------------------------------------------------

function UITool.ShowListHorScroll(ScrollViewObj,DataCount,CallBack)
    local SVWidth=ScrollViewObj:getContentSize().width
    local ItemSize=ScrollViewObj["ItemMode"]:getContentSize()
    local InnerWidth=math.max(SVWidth,ItemSize.width*DataCount)
    if ScrollViewObj["DataCount"]~=DataCount then --先只考虑刷新前后数量不变的情况--
        ScrollViewObj:setInnerContainerSize(cc.size(InnerWidth,ItemSize.height)) --改变会触发onEvent事件--
    end
    ScrollViewObj["DataCount"]=DataCount
    ScrollViewObj:removeAllChildren()

    local X=ScrollViewObj:getInnerContainerPosition().x
    ScrollViewObj:setInnerContainerPosition(cc.p(math.min(0,X),0))

local MaxItemCount=math.ceil(SVWidth/ItemSize.width)+1
    ScrollViewObj["ShowItemObjArr"]=ScrollViewObj["ShowItemObjArr"] or {}
    local ItemCount=table.nums(ScrollViewObj["ShowItemObjArr"]) --已有--
    for i=ItemCount+1,math.min(MaxItemCount,DataCount) do
        local ItemObj=ScrollViewObj["ItemMode"]:clone()
        ItemObj:setAnchorPoint(cc.p(0,0))
        ItemObj:retain()
        ItemObj:setContentSize(ItemSize)
        table.insert(ScrollViewObj["ShowItemObjArr"],ItemObj)
    end
    local PreIndex = 0

ScrollViewObj.onEventCallBack=function(e,ForceRefresh)
        local X=ScrollViewObj:getInnerContainerPosition().x
        local ScrollSpace=-1*X
        local CurIndex=math.ceil(ScrollSpace/ItemSize.width)
        CurIndex=math.max(1,CurIndex)
        if PreIndex~=CurIndex or ForceRefresh then
            PreIndex=CurIndex
            for i=1,table.nums(ScrollViewObj["ShowItemObjArr"]) do
                ScrollViewObj["ShowItemObjArr"][i]:setVisible(false)
                if CurIndex+i-1<=ScrollViewObj["DataCount"] then
                    ScrollViewObj["ShowItemObjArr"][i]:setVisible(true)

CallBack(CurIndex+i-1,ScrollViewObj["ShowItemObjArr"][i],ScrollViewObj["Data"][CurIndex+i-1])
                    ScrollViewObj["ShowItemObjArr"][i]:setPositionX(ItemSize.width*(CurIndex+i-2))
                    if not ScrollViewObj["ShowItemObjArr"][i]:getParent() then
                        ScrollViewObj:addChild(ScrollViewObj["ShowItemObjArr"][i])
                    end
                end
            end
        end
    end
    ScrollViewObj:onEvent(ScrollViewObj.onEventCallBack)
    ScrollViewObj.onEventCallBack()
end

-------------------------------------------------------------------------------------------------------------------------

function UITool.InitScrollViewPageMode(ScrollViewObj,BtnNextPage,BtnPrePage,CallBack)
    ScrollViewObj:jumpToTop()
    ScrollViewObj:setTouchEnabled(false)
    BtnNextPage:setVisible(true)
    BtnPrePage:setVisible(false)
    local SVHeight=ScrollViewObj:getContentSize().height
    local InnerHeight=ScrollViewObj:getInnerContainerSize().height
    local SumPage=math.ceil(InnerHeight/SVHeight)
    ScrollViewObj.getCurPageNum=function()
        local Y=ScrollViewObj:getInnerContainerPosition().y
        local ScrollSpace=InnerHeight+Y-SVHeight
        return math.floor(ScrollSpace/SVHeight)+1 
    end

ScrollViewObj.onPage=function(n)
        local Y=ScrollViewObj:getInnerContainerPosition().y
        ScrollViewObj:setInnerContainerPosition(cc.p(0,Y+SVHeight*n))
    end
    function HandleBtnVisible()
        local CurPageNum=ScrollViewObj.getCurPageNum()
        BtnNextPage:setVisible(CurPageNum<SumPage)
        BtnPrePage:setVisible(CurPageNum>1)
        if CallBack then
            CallBack(CurPageNum,SumPage)
        end
    end

BtnNextPage:addClickEventListener(function()
        ScrollViewObj.onPage( 1)
        HandleBtnVisible()
    end)
    BtnPrePage:addClickEventListener(function()
        ScrollViewObj.onPage(-1)
        HandleBtnVisible()
    end)
    if CallBack then
        CallBack(1,SumPage)
    end
end

东西不是很多,看一下就明白了,其中有一些是关于ScrollView的封装,这部分还是不错的,用得上的朋友还是可以用一下的,做个参考吧。 checkInvalidChar 是用来检测非法字的,这个还是很常见的,名字啊,聊天啊可能都能用得上