【问题标题】:cocos2d-x use lua, how to inheritance a c++ class and overwirite it's funcitonscocos2d-x使用lua,如何继承一个c++类并覆盖其功能
【发布时间】:2017-08-15 11:34:43
【问题描述】:

我要定义一个lua表,继承c++类cc.Node,并覆盖getContentSize()函数,返回一个新的contentsize。

-- here, how to inheritance cc.Node
local Ball = class('Ball')

function Ball:ctor()
end

function Ball:getContentSize()
     local width -- here, how to call super getContentSize
     local height
     return cc.size(width + 10, height + 10)
end

return Ball

【问题讨论】:

    标签: c++ lua cocos2d-x


    【解决方案1】:

    lua 表是一种有趣的类型,你可以这样做。

    local Ball = class('Ball', function() return cc.Node:create() end)
    

    但是继承c++类不支持super操作。你可以像这样调用超级函数。

    local _getContentSize = cc.Node.getContentSize
    
    function Ball:getContentSize()
        local width = _getContentSize(self).width
        local height = _getContentSize(self).height
        return cc.size(width + 10, height + 10)
    end
    

    即使您可以扩展 cc.Node 而无需继承。

    local Ball = cc.Node
    function Ball:newfunc()
    end
    

    你可以在lua的所有cc.Node对象中调用newfunc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2014-08-11
      • 2011-05-23
      相关资源
      最近更新 更多