Cocos + Lua realizes the expansion of existing component attribute methods

                     Cocos + Lua realizes the expansion of existing component attribute methods


 

table of Contents

1. Blog introduction 

2. Content

(1) Thinking

(2) Practical operation

3. Push

4. Conclusion


1. Blog introduction 

        I have been using cocos+lua to write things recently. I used lua seldom before. I found some writing methods in the project and thought it was good. Share here. The idea of ​​this article is somewhat similar to the static expansion in Unity. They are all extending the properties and methods of existing components. Compared with the static expansion in Unity, this expansion of lua should be more open.


2. Content

(1) Thinking

       Let me talk about the idea first. The reason is very simple. The table in lua has a very wide range of functions. We can add properties and methods to the table arbitrarily. For the components in Cocos, such as Label, Button, etc., the methods and properties are all It is encapsulated. If we want to expand, we can create a new table, add the required attributes and methods to this table, and then assign all the information of this table to the Label or Button that needs to be operated to achieve the corresponding Extended operations

(2) Practical operation

       A specific small example is prepared here. We often have some operations on the Label display in our game. For example, we need a certain label to display the number change from 1 to 100. At this time, we will use a timer or a callback or write To complete the entire operation with one action, we will use the expansion method to encapsulate the operation of increasing the number to facilitate future reuse.

 

First, we write a table and implement the logic of digital growth in the table method:

--拓展所用的表
addFunc               =   {}
addFunc.updateNum     = 0
--增长数字的方法  startNum:起始数字  targetNum:终点数字  durTime:时间   interal:每次变化间隔
function addFunc:updateLabelByNum(startNum,targetNum,durTime,interal)
	addFunc.updateNum = startNum
	--变化的总量
	local total = targetNum - startNum
	--变化的次数
	local changeNum = durTime/interal
	--单次变化的量
	local singleNum = total/changeNum
	self:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.DelayTime:create(interal), cc.CallFunc:create(function()
        	addFunc.updateNum = addFunc.updateNum + singleNum
        	self:strString(tostring(self.updateNum))
        	if addFunc.updateNum>= targetNum then
        		self:strString(tostring(targetNum))
        		self:stopAllActions()
        	end
    end)))) 
end

Then we implement an assignment method:

function inherit( sub, base )
    for k,v in pairs(base) do
        if sub[k] == nil then
            sub[k] = v
        end
    end
    return sub
end

Finally, when we use the Label component, we inherit the extension method we wrote:

    inherit(theLabel, addFunc)

When you need to increase the number, you can directly call the expansion method:

--Label从1变化到100,历经5秒,每次变化间隔1/20秒
theLabel:updateLabelByNum(1,100,5,1/20)

3. Push

Github:https://github.com/KingSun5


4. Conclusion

        If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/104898767