cocos2dx的extern.lua中的class方法为lua扩展了面向对象的功能,这使我们在开发中可以方便的继承原生类

但是用function返回对象的方法来继承C++类是没有super字段的,这使得在需要重写父类方法的时候没办法调用父类方法

在某位大神的博文中看到的代码

local _setVisible = nil
local MyLayer = class("MyLayer", function()
local layer = CCLayer:create()
-- save c++ member method point.
_setVisible = layer.setVisible
return layer
end)
-- override CCLayer::setVisible
function MyLayer:setVisible(visible)
-- invoke CCLayer::setVisible
_setVisible(self, visible)
-- to do something.
end
return MyLayer

下面这种方法是在metatable中一层一层递归的找指定函数,比上面那种优美很多

local function getSuperMethod(table, methodName)
local mt = getmetatable(table)
local method = nil
while mt and not method do
method = mt[methodName]
if not method then
local index = mt.__index
if index and type(index) == "function" then
method = index(mt, methodName)
elseif index and type(index) == "table" then
method = index[methodName]
end
end
mt = getmetatable(mt)
end
return method
end
local MyLayer = class("MyLayer", function()
return CCLayer:create()
end)
-- override CCLayer::setVisible
function MyLayer:setVisible(visible)
-- invoke CCLayer::setVisible
getSuperMethod(self, "setVisible")(self, visible)
-- to do something.
end
return MyLayer

在lua中继承cocos2dx的C++类还是有诸多不便的,有些时候用装饰者模式更好

cocos2dx lua中继承与覆盖C++方法的更多相关文章

  1. Cocos2d-x Lua中帧动画

    帧动画就是按一定时间间隔.一定的顺序.一帧一帧地显示帧图片.我们的美工要为精灵的运动绘制每一帧图片,因此帧动画会由很多帧组成,按照一定的顺序切换这些图片就可以了. 在Cocos2d-x Lua中播放帧 ...

  2. Cocos2d-x Lua中使用标签

    游戏场景中的文字包括了静态文字和动态文字.静态文字如下图所示游戏场景中①号文字“COCOS2DX”,动态文字如图4-1所示游戏场景中的②号文字“Hello World”.静态文字一般是由美工使用Pho ...

  3. cocos2d-x lua 中使用protobuf并对http进行处理

    cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...

  4. Lua中的元表与元方法学习总结

    前言 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了"+"符号,就可以进行类的加法运算.在Lu ...

  5. cocos2d-x lua中实现异步加载纹理

    原文地址:  http://www.cnblogs.com/linchaolong/p/4033118.html 前言   问题:最近项目中需要做一个loading个界面,界面中间有一个角色人物走动的 ...

  6. cocos2dx lua中异步加载网络图片,可用于显示微信头像

    最近在做一个棋牌项目,脚本语言用的lua,登录需要使用微信登录,用户头像用微信账户的头像,微信接口返回的头像是一个url,那么遇到的一个问题就是如何在lua中异步加载这个头像,先在引擎源码里找了下可能 ...

  7. Lua中的元表与元方法

    [前言] 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了“+”符号,就可以进行类的加法运算.在Lua中也有这个道理 ...

  8. Lua中的元表和元方法

    Lua中每个值都可具有元表. 元表是普通的Lua表,定义了原始值在某些特定操作下的行为.你可通过在值的原表中设置特定的字段来改变作用于该值的操作的某些行为特征.例如,当数字值作为加法的操作数时,Lua ...

  9. java怎么调用子类中父类被覆盖的方法

    public class b { { void show() { System.out.println("b"); } } public class c extends b { v ...

随机推荐

  1. Centos环境下部署游戏服务器-Eclipse

    一直在想这篇文章该不该写,因为这篇文章更像是教你如何使用一个ide这种文章,毫无价值可言.但思来想去还是应给写.上篇文章主要说了编译原理和过程,这篇文章就是理论联系实际的典范.并且很多工程师一辈子都不 ...

  2. 280. Wiggle Sort

    题目: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] ...

  3. Java:IO流之转换流

    IO流的基本流情况: 字符流:                字符流缓冲区: FileReader              BufferedReader FileWriter             ...

  4. OpenCV Manager package was not found

    http://www.tuicool.com/articles/322Yj2 !! [1]的底部就有解决办法,一般“\OpenCV-2.4.6-android-sdk\apk”这个路径下就是 Open ...

  5. hdu1005 Number Sequence(数论)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. Win XP 如何禁用系统的自动更新

    想关闭系统的自动更新. 打开[控制面板]/[安全中心],发现“自动更新”和“更改安全中心通知我的方式”,都已成了灰色,无法更改. 网上查了一下,找到了这样一个处理方法:将[服务]中一个名为“Autom ...

  7. 8.cadence.CIS[原创]

    一.CIS数据库配置 ------ ---------------------------- --------------- ------------------ ---- 二.CIS放置元件 --- ...

  8. .NET_Framework_version_history

    http://en.wikipedia.org/wiki/.NET_Framework_version_history

  9. ssh: Could not resolve hostname gitcafe.com: nodename nor servname provided, or not known

    今天 git push 的时候报如下错误: ssh: Could not resolve hostname gitcafe.com: nodename nor servname provided, o ...

  10. d is undefined错误

    如图这样的错误:TypeError : d is undefined,今天早上在写代码的时候也是遇见这样的错误,报告的错误是jquery.js中错误,但是这样的问题经常是由于什么名字不对(个人见解), ...