cocos2d-x使用lua,如何继承一个c++class并覆盖其函数

cocos2d-x use lua, how to inheritance a c++ class and overwirite it's funcitons

我想定义一个luatable,继承c++classcc.Node,覆盖getContentSize()函数,return一个新内容大小。

-- 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

lua table是个有趣的类型,你可以这样做。

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

但是继承c++class不支持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