Sakura Sakura -4 years ago 93
C++ Question

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

I want to define a lua table, inheritance c++ class

cc.Node
, and overwirite the
getContentSize()
function, return a new 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

Answer Source

lua table is a interesting type, you can do like this.

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

but inheritance c++ class is not support super operate. you can call super funciton like this.

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

even you can extend cc.Node without inheritance.

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

you can call newfunc in all cc.Node object in lua.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download