[寒江孤叶丶的Cocos2d-x之旅_37]获取LUA的父类方法

前端之家收集整理的这篇文章主要介绍了[寒江孤叶丶的Cocos2d-x之旅_37]获取LUA的父类方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列]

博客地址:http://blog.csdn.net/qq446569365

在LUA中,重写父类函数之后有时候需要调用父类方法,LUA调用父类方法要比C++中略为复杂一些,如下:

  1. --Test:APUtils.getSuperMethod(self,"setPosition")(self,p) --调用父类方法并传递参数
  2. --获取父类函数
  3. function _G.getSuperMethod(table,methodName)
  4. local mt = getMetatable(table)
  5. local method = nil
  6. while mt and not method do
  7. method = mt[methodName]
  8. if not method then
  9. local index = mt.__index
  10. if index and type(index) == "function" then
  11. method = index(mt,methodName)
  12. elseif index and type(index) == "table" then
  13. method = index[methodName]
  14. end
  15. end
  16. mt = getMetatable(mt)
  17. end
  18. return method
  19. end

猜你在找的Cocos2d-x相关文章