quick: iskindof使用注意

quick: iskindof使用注意

--[[--

如果对象是指定类或其子类的实例,返回 true,否则返回 false

~~~ lua

local Animal = class("Animal")
local Duck = class("Duck", Animal)

print(iskindof(Duck.new(), "Animal")) -- 输出 true

~~~

@param mixed obj 要检查的对象
@param string classname 类名

@return boolean

]]
function iskindof(obj, classname)
    local t = type(obj)
    local mt
    if t == "table" then
        mt = getmetatable(obj)
    elseif t == "userdata" then
    		-- ** 下面这行是我仿照cocos2d-x-3.11.1新加的,用于判断C++类的继承关系,如cc.Node
        if tolua.iskindof(obj, classname) then return true end
        
        mt = tolua.getpeer(obj)
    end

    while mt do
        if mt.__cname == classname then
            return true
        end
        mt = mt.super
    end

    return false
end
 printf("UIListView ==== " .. tostring(iskindof(self.lvGrid, "UIListView")))
    printf("UIScrollView ==== " .. tostring(iskindof(self.lvGrid, "UIScrollView")))
    printf("cc.Node ==== " .. tostring(iskindof(self.lvGrid, "cc.Node")))
    printf("cc.ClippingRegionNode ==== " .. tostring(iskindof(self.lvGrid, "cc.ClippingRegionNode")))
    printf("cc.ClippingRectangleNode ==== " .. tostring(iskindof(self.lvGrid, "cc.ClippingRectangleNode")))
    printf("__cname ==== " .. tostring(self.lvGrid.__cname))
    
   
输出结果:    
[LUA-print] UIListView ==== true
[LUA-print] UIScrollView ==== true
[LUA-print] cc.Node ==== true
[LUA-print] cc.ClippingRegionNode ==== false
[LUA-print] cc.ClippingRectangleNode ==== true
[LUA-print] __cname ==== UIListView

注意:

但是因为quick不能判断继承自C++如Node的情况,我实验了cocos2d-x-3.11.1中的class和iskindof方法,发现方法有很大的不同,但是printf("UIListView ==== " .. tostring(iskindof(self.lvGrid, "UIListView")))这种都判断为false的结果,网上有一篇博客修改了,http://m.blog.csdn.net/article/details?id=49799637但是我测试了下还是有问题,不过没有详细测试,最终决定还是使用quick中的class 和 iskindof方法

posted on 2016-10-27 10:48  ZhYQ_note  阅读(582)  评论(0编辑  收藏  举报

导航