在lua中使用類對象很簡單也很方便,下面以創建精靈爲例
require "Cocos2d"
require "Cocos2dConstants"
--精靈類測試
local testSprite = class("testSprite",function (name)
return cc.Sprite:create(name)
end)
function testSprite:createSprite(name)
--創建精靈
local sp = testSprite.new(name)
return sp
end
--返回精靈類對象
return testSprite
在main.lua中這麼調用
local tests=require("testSprite")
local sp3=tests:createSprite("land.png")
sp3:setPosition(180,250)
layer:addChild(sp3,1)