cocos2dx自带的CCBProxy真弱,还好提供了一个CCBReaderLoader.lua,但是也不好用,

于是修改了一下CCBReaderLoader,下面直接贴代码了。

function NewCCBuilderReaderLoad(strFilePath,proxy,owner)
if nil == proxy then
return
end --print("ccbnew")
local ccbReader = proxy:createCCBReader()
local node = ccbReader:load(strFilePath)
local rootName = "" if nil ~= owner then
--Callbacks
--print("ccb new callback")
local ownerCallbackNames = tolua.cast(ccbReader:getOwnerCallbackNames(),"CCArray")
local ownerCallbackNodes = tolua.cast(ccbReader:getOwnerCallbackNodes(),"CCArray")
local ownerCallbackControlEvents = tolua.cast(ccbReader:getOwnerCallbackControlEvents(),"CCArray")
local i =
--print("ccb 222",ownerCallbackNames:count())
for i = ,ownerCallbackNames:count() do
local callbackName = tolua.cast(ownerCallbackNames:objectAtIndex(i - ),"CCString")
local callbackNode = tolua.cast(ownerCallbackNodes:objectAtIndex(i - ),"CCNode")
--print("ccb333",callbackName)
if "function" == type(owner[callbackName]) then
local integerValue = tolua.cast(ownerCallbackControlEvents:objectAtIndex(i - ),"CCInteger")
if nil ~= integerValue then
proxy:setCallback(callbackNode, owner[callbackName], integerValue:getValue())
end
else
--print("Warning: Cannot find owner's lua function:" .. ":" .. callbackName .. " for ownerVar selector")
end end --Variables
local ownerOutletNames = tolua.cast(ccbReader:getOwnerOutletNames(),"CCArray")
local ownerOutletNodes = tolua.cast(ccbReader:getOwnerOutletNodes(),"CCArray")
for i = , ownerOutletNames:count() do
local outletName = tolua.cast(ownerOutletNames:objectAtIndex(i - ),"CCString")
local outletNode = tolua.cast(ownerOutletNodes:objectAtIndex(i - ),"CCNode")
owner[outletName:getCString()] = outletNode
end
end local nodesWithAnimationManagers = tolua.cast(ccbReader:getNodesWithAnimationManagers(),"CCArray")
local animationManagersForNodes = tolua.cast(ccbReader:getAnimationManagersForNodes(),"CCArray") --print("cccb 44444",nodesWithAnimationManagers:count())
for i = , nodesWithAnimationManagers:count() do
local innerNode = tolua.cast(nodesWithAnimationManagers:objectAtIndex(i - ),"CCNode")
local animationManager = tolua.cast(animationManagersForNodes:objectAtIndex(i - ), "CCBAnimationManager")
local documentControllerName = animationManager:getDocumentControllerName()
--print("ccb 555",documentControllerName)
if "" == documentControllerName then end
----print("ccbcall",owner.ccbCall[documentControllerName][])
if nil ~= documentControllerName then
owner.ccbCall["mAnimationManager"] = animationManager
end --Callbacks
local documentCallbackNames = tolua.cast(animationManager:getDocumentCallbackNames(),"CCArray")
local documentCallbackNodes = tolua.cast(animationManager:getDocumentCallbackNodes(),"CCArray")
local documentCallbackControlEvents = tolua.cast(animationManager:getDocumentCallbackControlEvents(),"CCArray") for i = ,documentCallbackNames:count() do
local callbackName = tolua.cast(documentCallbackNames:objectAtIndex(i - ),"CCString")
local callbackNode = tolua.cast(documentCallbackNodes:objectAtIndex(i - ),"CCNode")
if "" ~= documentControllerName then
local cbName = callbackName:getCString()
--print("ccccccb",owner)
if "function" == type(owner.ccbCall[cbName]) then
local integerValue = tolua.cast(documentCallbackControlEvents:objectAtIndex(i - ),"CCInteger")
if nil ~= integerValue then
proxy:setCallback(callbackNode, owner.ccbCall[cbName], integerValue:getValue())
end
else
print("Warning: Cannot found lua function [" .. documentControllerName .. ":" .. callbackName:getCString() .. "] for docRoot selector")
end
end
end --Variables
local documentOutletNames = tolua.cast(animationManager:getDocumentOutletNames(),"CCArray")
local documentOutletNodes = tolua.cast(animationManager:getDocumentOutletNodes(),"CCArray") for i = , documentOutletNames:count() do
local outletName = tolua.cast(documentOutletNames:objectAtIndex(i - ),"CCString")
local outletNode = tolua.cast(documentOutletNodes:objectAtIndex(i - ),"CCNode") if nil ~= documentControllerName then
owner.ccbCall[outletName:getCString()] = tolua.cast(outletNode, proxy:getNodeTypeName(outletNode))
end
end --Setup timeline callbacks
local keyframeCallbacks = animationManager:getKeyframeCallbacks() for i = , keyframeCallbacks:count() do
local callbackCombine = tolua.cast(keyframeCallbacks:objectAtIndex(i - ),"CCString"):getCString()
local beignIndex,endIndex = string.find(callbackCombine,":")
local callbackType = tonumber(string.sub(callbackCombine,,beignIndex - ))
local callbackName = string.sub(callbackCombine,endIndex + , -)
--Document callback if == callbackType and nil ~= documentControllerName then
local callfunc = CCCallFunc:create(owner.ccbCall[callbackName])
animationManager:setCallFuncForLuaCallbackNamed(callfunc, callbackCombine)
elseif == callbackType and nil ~= owner then --Owner callback
local callfunc = CCCallFunc:create(owner[callbackName])
animationManager:setCallFuncForLuaCallbackNamed(callfunc, callbackCombine)
end
end
--start animation
local autoPlaySeqId = animationManager:getAutoPlaySequenceId()
if - ~= autoPlaySeqId then
animationManager:runAnimationsForSequenceIdTweenDuration(autoPlaySeqId, )
end
end return node
end ComView=class("ComView", function ( )
--print("comview class")
return display.newNode();
end) ComView.ccbCall={}
function ComView:ctor(args) local proxy = CCBProxy:create()

   --2014.7.30 修正,如果不用元表,就会出现同名变量被覆盖的问题
    self.ccbCall={}

    setmetatable(self.ccbCall, {__index=ComView.ccbCall})
     self.ccbNode=NewCCBuilderReaderLoad(args.f,proxy,self)

if args.add== nil or args.add==true then
self:addChild(self.ccbNode)
end
end

用法如下:

local XxxView = class("XxxView", ComView)--必须继承ComView

function XxxView:ctor()

        self.ccbCall["menu"]=handler(self,self.menu_click)--配置ccb里的回调函数,必须写在self.ccbCall表里,表里的key是ccb里的名字,对应的值是类里的函数,要用handler包装一下    

    XxxView.super.ctor(self,{f="XxxView.ccbi"}) --必须在配置回调函数后在调用父类的构造函数

        --此时可以直接用self.ccbNode操作读入的ccb

        --ccb里的变量可以直接用self.ccbCall[变量名]访问,类型不需要tolua.cast

end

function XxxView:menu_click(tag,sender)

end

注意XxxView实际是一个node,ccb是它的子节点

XxxView.super.ctor(self,{f="XxxView.ccbi"}),还可以传入一个add参数,如果add为nil或true则自动把ccb节点加到XxxView中,

其它不加,方便在XxxView里控制加载ccb的时机。

注意ccb工程要按如下设置:

改ccb要设置为js的

ccb里的根节点要设置一个js controller名字随意,如果没有设置,会有nodesWithAnimationManagers相关的错误。

或者直接把整个工程设为js的,这样新建的ccb都是js的了  菜单位置“File--Project settings"

最新文章

  1. 开源日志记录工具log4j
  2. QT文件读写
  3. java 中LinkedList的学习
  4. BZOJ 1191: [HNOI2006]超级英雄Hero 二分匹配
  5. zend studio常见问题解答
  6. argparse - 命令行选项与参数解析(转)
  7. JDBC之数据库操作
  8. css笔记——css 实现自定义按钮
  9. delphi xe5 android 使用样式(风格)
  10. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)
  11. Dynamics CRM JS的调试的弊端解决办法
  12. 18.12.09-C语言练习:兔子繁衍问题 / Fibonacci 数列
  13. 05. .stop、.prevent、.capture、.self、.once、
  14. Python语言的高级特性
  15. shell脚本之分析oracle数据库数据泵日志中表的大小
  16. nodejs项目安装ant design
  17. $(document).ready和window.onload 简单分析区别
  18. fedora开机出现There is a problem with the configuration server. (/usr/libexec/gconf-sanity-check-2 exited with status 256)
  19. linux环境中安装ftp服务
  20. poj 1274 The Prefect Stall - 二分匹配

热门文章

  1. php的opcache缓存扩展(php页面代码刷新速度)
  2. leetCode 86.Partition List(分区链表) 解题思路和方法
  3. HDU 1710 Binary Tree Traversals(二叉树)
  4. Asp.net视图状态的作用
  5. NET设计规范二:类型成员设计
  6. FastDFS简易概括
  7. MyEclipse连接sqlserver2008具体流程
  8. 微信小程序之底部弹框预约插件
  9. iDempiere VS World
  10. C#:(问题)已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭