cocos2dx之lua派生类和方法重新

记得把extern.lua拷贝到你的资源目录,这里要用到

require "extern"

MyLayer = class("MyLayer",
    function(fileName)
        return CCSprite:create(fileName) 
    end
)

function MyLayer.CreateWithFile(fileName)
    local o = MyLayer.new(fileName)
    o.m_nA = 0
    return o
end

function MyLayer:setVisible(flag)
    getmetatable(self).setVisible(self,flag)
    self.m_nA = self.m_nA + 5
end

local a = MyLayer.CreateWithFile("dog.png")
print(a.m_nA)


local b = MyLayer.CreateWithFile("dog.png")
b:setVisible(true)
print(b.m_nA)

 

顺道记一下lua里有变长参数的函数用法

function Add(...)
    local t = {...}
    for i,v ipairs(t) do
        print(i,v)
    end
end

 

posted @ 2014-03-20 18:36  MrBlue  阅读(504)  评论(0编辑  收藏  举报