cocos2dx游戏开发学习笔记3-lua面向对象分析
来源:互联网 发布:电脑上能看淘宝直播吗 编辑:程序博客网 时间:2024/04/28 12:56
在lua中,可以通过元表来实现类、对象、继承等。与元表相关的方法有setmetatable()、__index、getmetatable()、__newindex。
具体什么是元表在这里就不细说了,网上很多介绍,这里主要讲与cocos2dx相关联的部分。
在lua-binding库中extern.lua里,有如下方法:
--Create an class.function class(classname, super) local superType = type(super) local cls if superType ~= "function" and superType ~= "table" then superType = nil super = nil end if superType == "function" or (super and super.__ctype == 1) then -- inherited from native C++ Object cls = {} if superType == "table" then -- copy fields from super for k,v in pairs(super) do cls[k] = v end cls.__create = super.__create cls.super = super else cls.__create = super end cls.ctor = function() end cls.__cname = classname cls.__ctype = 1 function cls.new(...) local instance = cls.__create(...) -- copy fields from class to native object for k,v in pairs(cls) do instance[k] = v end instance.class = cls instance:ctor(...) return instance end else -- inherited from Lua Object if super then cls = clone(super) cls.super = super else cls = {ctor = function() end} end cls.__cname = classname cls.__ctype = 2 -- lua cls.__index = cls function cls.new(...) local instance = setmetatable({}, cls) instance.class = cls instance:ctor(...) return instance end end return clsend通过该方法,我们可以很方便的定义一个class、继承一个class。
定义一个类:
MyClass = class("MyClass")function MyClass:ctor()print("MyClass:ctor()")endlocal myclass = MyClass:new()
继承一个类:
GameLayer = class("GameLayer", function()local layer = cc.Layer:create()return layerend)local gamelayer = GameLayer:new()addChild(gamelayer)
0 0
- cocos2dx游戏开发学习笔记3-lua面向对象分析
- cocos2dx +lua 面向对象
- Lua学习笔记:面向对象
- Lua学习笔记--面向对象
- Cocos2d-x Lua游戏开发之Lua 面向对象编程
- Lua学习笔记Day3-Lua实现类、Lua面向对象
- COCOS2DX,LUA,学习笔记
- 14.lua学习笔记:面向对象
- 161218 lua学习 --代码+笔记 《面向对象》
- Lua学习笔记之面向对象
- cocos2dx游戏开发学习笔记1-基本概念
- cocos2dx游戏开发学习笔记(一)
- cocos2dx游戏开发学习笔记(二)
- cocos2dx游戏开发学习笔记(三)
- Lua学习笔记七——lua也面向对象
- 【cocos2d-x3.2游戏开发】 lua 类, 继承, 面向对象
- 【cocos2d-x3.2游戏开发】 lua 类, 继承, 面向对象
- lua面向对象学习
- matlab中常用函数2
- PHP中的符号 ->、=> 和 :: 分别表示什么意思?
- 每个程序员都必读的10篇重要文章
- LeetCode 014 Longest Common Prefix
- lua调用c函数
- cocos2dx游戏开发学习笔记3-lua面向对象分析
- 在C语言中,double、long、unsigned、int、char类型数据所占字节数
- mysql deadlocks with concurrent inserts
- printf函数
- 邮件传输协议SMTP,POP3,IMAP,MIME
- 为现代JavaScript开发做好准备
- C++编程对缓冲区的理解
- 租用服器WIN2003+IIS6+PHP5.4 +PHP5.3.8的安装配置
- 802.1x协议浅析