【步兵 lua】利用腳本的優勢

【步兵 lua】利用腳本的優勢 by EOS.

剛開始從c++轉到lua的同學,可能會因爲習慣了嚴格的寫法,
從而使得腳本很多的靈活之處沒有被髮揮出來,那麼就讓我來教壞你吧。
(ps:腳本用多了不知道會不會回不去了= =、,好久沒碰C艹了)


參數

--參數可以是不確定類型的
function node:setSize(size, height)
    if height then
        local width = size
        self.size.width = width
        self.size.height = height
    else
        self.size = size
    end
end

--當然你也可以用type來區別做一些處理
function node:setTag(tagOrName)
    if type(tagOrName) == "string" then
        node:setName(tagOrName)
    else
        node:setTag(tagOrName)
    end
end

--又或者不確定個數
function newButton(spNormal, spSelected, clickEvent)
    if type(spSelected) == "function" then
        clickEvent = spSelected
        spSelected = nil
    end
    --...
end

表拓展

-- 類拓展
local expandNode = function(node)
    function node:addTo(paret, layout, offset, zorder, tagOrName)
        print("papapa~")
        -- 邏輯省略
    end
end

ess = function(width, height)
    return {width = width, height = height}
end

newNode = function(size)
    local ret = { 
        size = {width = 0, height = 0} 
    }
    if size then ret.size = size end
    expandNode(ret)--拓展node
    return ret
end

--測試
print"nodeTest:"
local node = newNode(ess(100, 200))
print(node.size.width)
node:addTo(xx)

運行結果:
這裏寫圖片描述

比如說我,就直接把一個button,然後加表拓展,成功的實現了標籤頁的功能。
再比如,創建一個列表的cell,然後直接 function cell:fresh(data),很方便。
但是一定要在高內聚的前提下,不要東邊加一個東西,過一會西邊又加一個東西,
這簡直是在作死=、=(no zuo no die, don’t try)


用表來做參數

newLabel_deprecation = function(input, fontName, fontSize, fontColor, labelSize)

end

假如參數是這樣的話,那麼我創建可能每次都輸入 fontName
即使有默認值 fontSize = fontSize or 32
那也要 newLabel_deprecation(“123”, nil, nil, “ff0000”)
這樣看起來並不舒服,(當然也可以選擇二次封裝)
所以當參數過多時候,順序結構就限制了可讀性

下邊就是結合前邊的綜合應用了。

-- lua lu

-- 類拓展
local expandNode = function(node)
    function node:addTo(paret, layout, offset, zorder, tagOrName)
        print("papapa~")
        if type(paret) == "table" then
            local tb = paret
            paret, layout, offset, zorder, tagOrName
            = tb[1], tb[2], tb[3], tb[4], tb[5], tb[6]
            print(paret, layout, offset, zorder, tagOrName)
        end
        -- 邏輯省略
    end

    -- 參數可以是不確定類型的
    function node:setSize(size, height)
        print("ssssss~")
        if height then
            local width = size
            self.size.width = width
            self.size.height = height
        else
            self.size = size
        end
    end
end


--用表當參數
print"labelTest:"

--映射字典
local mapDic = {
    size = "setSize",
    addTo = "addTo"
    --..其他就不列舉了
}
newLabel = function(args)
    local ret = newNode()
    for k,v in pairs(args) do
        ret[mapDic[k]](ret, v)
    end
    return ret
end

local label = newLabel{
    size = ess(20, 2016),
    addTo = {"parentNode", "acc", "nil", "-1", "testLabel"}
    --..其他屬性省略
}
--ps:鏈式編碼 say gu bye~(假裝說再見=、=)
print(label.size.height)

運行結果:
這裏寫圖片描述


另闢蹊徑

把東西功能內聚起來~

function Layer:initNet()
    -------------FUNC1---------------
    function self:reqs_Fuc1()

    end

    local function recv_Fuc1()

    end
    Net:addListener(Code.Fuc1,recv_Fuc1)

    -------------FUNC2---------------
    --...
end

又或者這麼玩?

print""
print"PlayerTest:"
local AbilityDic = {
    Fly = 1
}
newPlayer = function(name)
    local ret = {}
    ret.name = name
    function ret:walk()
        print(self.name .. " walk")
    end

    function ret:canFly()
        return self.fly ~= nil
    end

    function ret:expand(AbilityTag)
        if AbilityTag == AbilityDic.Fly then
            if self:canFly() then return end
            function ret:fly()
                print(self.name .. " fly")
            end
        end
    end


    return ret
end

local eos = newPlayer("eos")
eos:walk()
print(eos:canFly() and "can fly" or "can't fly")

eos:expand(AbilityDic.Fly)
print"test again"
print(eos:canFly() and "can fly" or "can't fly")
eos:fly()

--當然也可以這樣,假裝在寫js、、 = 3=
_= eos.fly and eos:fly()

運行結果:
這裏寫圖片描述


結語

可能還有想到的地方,暫時就這樣,如果以後再有什麼好想法,繼續出(續)~
話說這篇已經不短了=、= 那麼!

See Again~
之前
真愛無價,歡迎打賞~
讚賞碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章