【学习笔记】【Cocos2d-x Lua脚本开发】Lua中创建自定义类

时间:2023-02-06 19:12:28

Lua中创建自定义类

Lua自定义类:一个重要的文件extern.lua

--Create an class.
function class(classname, super)
--获取super类型
local superType = type(super)
--cls是最终创建出来的class
local cls

--[[
-----------------------------------------------------------------
构造class的方式主要体现为两种:
1.通过传入一个function类型的函数;
2.通过传入一个table类型的表;
因此,在次方法的讨论中,主要围绕着super的类型进行展开
-----------------------------------------------------------------
说明:
1.在Cocos2d-x2.1.3引擎,TestLua下的Ball.lua文件中有
通过传入function类型函数来创建Ball对象的示例:

--继承部分 begin
Ball = class("Ball", function(texture)
return CCSprite:createWithTexture(texture)
end)
Ball.__index = Ball
--继承部分 end

--子类的成员数据、方法定义 begin
function Ball:radius()
--implementaion
end

--Ball的实例创建方法
function Ball.ballWithTexture(aTexture)

if(aTexture == nil) then
cclog("in ballWithTexture aTexture == nil")
end

local ball = Ball.new(aTexture)
ball:autorelease()
return ball
end
--others
--子类的成员数据、方法定义 end

2.当然,也有可能已经自定义了一个Lua类型的“基类”对象,它是table类型,
比如说role = { move = function print("move!") end},那么,
有如下的继承方式:
Ball = class("Ball", role)
-----------------------------------------------------------------
--]]
--接下来看看怎么实现的
--首先,对super类型进行判断,主要围绕着function类型和table类型展开
--如果superType不是这两种类型之一,那么superType和super被设置为nil
if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end

--如果是利用函数创建或者是继承自一个table类型的类(table类型的类的__ctype是1)
if superType == "function" or (super and super.__ctype == 1) then
-- inherited from native C++ Object
cls = {}

--如果super是table类型,则根据table给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
--否则使用super作为cls的__create键的值
cls.__create = super
end

--无论是function类型还是table类型,都设置以下键值
cls.ctor = function() end
cls.__cname = classname
cls.__ctype = 1

--设置cls的new函数
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
--所有的super为nil的情况(即非function/table类型)会跳转至此
cls = {ctor = function() end}
end
--super为nil的情形下,还会增加如下键值
cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls

--同时设置new函数
function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end

return cls
end

--让node对象循环执行一个sequence动作,这个动作由时间延迟和回调函数组成,完成schedule的设计
function schedule(node, callback, delay)
local delay = CCDelayTime:create(delay)
local callfunc = CCCallFunc:create(callback)
local sequence = CCSequence:createWithTwoActions(delay, callfunc)
local action = CCRepeatForever:create(sequence)
node:runAction(action)
return action
end

--delay延迟后,让node对象执行callback函数
function performWithDelay(node, callback, delay)
local delay = CCDelayTime:create(delay)
local callfunc = CCCallFunc:create(callback)
local sequence = CCSequence:createWithTwoActions(delay, callfunc)
node:runAction(sequence)
return sequence
end





创建自定义类DeapSeaSprite

注意,使用过程中需要向项目中添加extern.lua文件
--DeapSeaSprite.lua

require("extern")

--DeepSeaSprite
DeepSeaSprite = class("DeepSeaSprite", function(filename)
return CCSprite:createWithTexture(filename)
end)
--用于读访问索引
DeepSeaSprite.__index = DeepSeaSprite

function DeepSeaSprite.deepSeaSpriteWithTexture(filename)

if(filename == nil) then
print("in DeepSeaSprite.deepSeaSprite filename == nil")
end

local deepseasprite = DeepSeaSprite.new(filename)
return deepseasprite
end


自定义类使用

    --测试Lua中创建自定义类
--DeepSeaSprite.lua文件引入
require("DeepSeaSprite")

--使用addImage方法向纹理管理器中添加"land.png"图片,该调用会返回land.png图片的纹理
local texture = CCTextureCache:sharedTextureCache():addImage("land.png")

local dss001 = DeepSeaSprite.deepSeaSpriteWithTexture(texture)
dss001:setPosition(100, 100)
layerForWorld:addChild(dss001)


运行结果

【学习笔记】【Cocos2d-x Lua脚本开发】Lua中创建自定义类