cocos2d+lua绑定关系理解——一

通过lua代码往上层追溯,从整体上理解cocos2d+lua的接口设计思路和整套代码的运行流程。

首先来看一句简单的lua代码

local spr = cc.Sprite:create()

cc是一张表(table),表(table)是lua里面唯一的数据结构,cc存于_G中,_G是lua里面的全局表,cc表中存了所有cocos绑定的常量和“类”。

Sprite也是个表(table),Sprite表是怎样跟c++代码关联起来的呢,这就要分析c++的绑定代码了。首先来看看Sprite表里面的内容

["Sprite"] = {--table: 0x0032cce0
    [".isclass"] = "true",
    [".metatable"] = table: 0x0032c690
},

通过打印发现,Sprite表里面就一条数据key=[".isclass"] value=true,这个表明这个此table是cocos绑定的一个类型。看来秘密都在元表(metatable)里面。元表(metatable)在注册表(LUA_REGISTRYINDEX)里面,关于lua的全局表(_G)、注册表(Register)、元表(metatable),这里不再介绍了,不理解的需要先去学习一下。下面来打印一下Sprite的元表(metatable)里面的内容

["cc.Sprite"] = {--table: 0x0032c690
    ["getTextureRect"] = "function: 0x00b9b430",
    ["getBatchNode"] = "function: 0x00b9afe8",
    ["setTextureAtlas"] = "function: 0x00b9b770",
    ["new"] = "function: 0x00b9b168",
    ["setDisplayFrameWithAnimationName"] = "function: 0x00b9b750",
    ["create"] = "function: 0x003284a8",
    ["__lt"] = "function: 0x0032cbf0",
    ["getTexture"] = "function: 0x00b9b2b8",
    ["__sub"] = "function: 0x0032cc58",
    ["initWithFile"] = "function: 0x00b9b450",
    ["isTextureRectRotated"] = "function: 0x00b9b3e8",
    ["removeAllChildrenWithCleanup"] = "function: 0x00b9b050",
    ["setTextureRect"] = "function: 0x00b9b098",
    ["setDirty"] = "function: 0x00b9b398",
    ["getSpriteFrame"] = "function: 0x00b9b790",
    ["__newindex"] = "function: 0x0032c710",
    ["__eq"] = "function: 0x0032cc30",
    ["setPolygonInfo"] = "function: 0x00328488",
    ["initWithPolygon"] = "function: 0x00328440",
    ["__le"] = "function: 0x0032cc10",
    ["setFlippedX"] = "function: 0x00b9af60",
    ["createWithSpriteFrame"] = "function: 0x00b9be50",
    ["createWithSpriteFrameName"] = "function: 0x00b9be00",
    ["tolua_ubox"] = "table: 0x002c86f0" , -- loop table,
    ["createWithTexture"] = "function: 0x00b9b5c0",
    ["setTexture"] = "function: 0x00b9b298",
    ["setVertexRect"] = "function: 0x00b9b5a0",
    ["isFlippedY"] = "function: 0x00b9b558",
    ["isFlippedX"] = "function: 0x00b9b518",
    ["getResourceType"] = "function: 0x00b9afa8",
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值