改进cocos2dx中lua读ccb的方法
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"

改进cocos2dx中lua读ccb的方法的更多相关文章
- Learning Lua Programming (4) Cocos2d-x中Lua编程(一)
刚开始接触cocos2d-x 下的Lua编程,主要参看了李华明大神的博客中的介绍,http://blog.csdn.net/xiaominghimi/article/category/1155088 ...
- Cocos2d-x中获取设备语言的方法
1.cocos2dx获取设备语言的方法:CCApplication::sharedApplication()->getCurrentLanguage() 2.cocos2dx 2.1.4支持识别 ...
- cocos2dx中的场景和使用方法
1.一个游戏中有且只有一个导演,但是至少有一个场景 2.场景是游戏元素节点数的根节点,也可以理解为该场景下的渲染树的根节点 3.场景是一个容器,包含了该场景下的所有游戏元素,比如层,精灵 4.场景是导 ...
- cocos2dx中加载图片资源的方法,和从内存中获取已经加载的图片资源的方法
游戏中通常需要将常用的资源如:声音,图片,plist文件,提前加载进内存,以加快游戏的流畅度 1.预加载声音: SimpleAudioEngine::getInstance()->preload ...
- 关于Cocos2d-x中打包图集和使用方法
准备的过程 1.打开TextruePacker软件 2.把游戏中要使用的图片拖到TextruePacker里面,TextruePacker会自动帮我们排序,让所有小图变成一个大图 3.点击Publis ...
- 关于Cocos2d-x中的scheduleUpdate和update方法的使用
一.如果要让某类实例对象要连续执行某些语句(比如让每个Block实例从运行框最右边移动到最左边) 要在Block类中增加一些东西 1.先在其.cpp文件的init()函数中执行scheduleUpda ...
- cocos2dx之lua派生类和方法重新
记得把extern.lua拷贝到你的资源目录,这里要用到 require "extern" MyLayer = class("MyLayer", functio ...
- 关于Cocos2d-x中init方法和onEnter方法的区别
init()和onEnter()这两个方法都是写实例化对象的类(比如继承自Node的一些类等等)的时候用到的方法. 一般都是public类型下面的 bool init(); void onEnter( ...
- 关于Cocos2d-x中让主角运动的方法
比如要让角色跳起来 1.如果是用到物理引擎,那么在物理世界中,可以用 hero->getPhysicsBody()->setVelocity(Vec2(0, 400)); //给主角一个 ...
随机推荐
- SQL Alias
There are two types of aliases that are used most frequently in SQL command: which is column alias a ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
- ShareSDK for Android 2.3.10已经公布
ShareSDK for Android 2.3.10已经公布,本次更新内容包含: 1.加入自己定义分享标签功能 新版本号SDK下载页面地址: http://share.sharesdk.cn/Dow ...
- C++ 11 - STL - 函数对象(Function Object) (中)
我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the va ...
- JDBC-DAO经典模式 实现对数据库的增、删、改、查
JDBC(Java Data Base Connection)的作用是连接数据库 先看下jdbc连接SQLServer数据库的简单例子 代码实现(FirstJDBC): package com.jdb ...
- ant design pro (三)路由和菜单
一.概述 参看地址:https://pro.ant.design/docs/router-and-nav-cn 二.原文摘要 路由和菜单是组织起一个应用的关键骨架,我们的脚手架提供了一些基本的工具及模 ...
- webpack 引入jquery和第三方jquery插件
1.引入jquery jQuery 直接在 html 中引入,然后在 webpack 中把它配置为全局即可. index.html: <!DOCTYPE html> <html> ...
- CompletableFuture 详解
转 http://www.jianshu.com/p/6f3ee90ab7d3 CompletableFuture类实现了CompletionStage和Future接口.Future是Java 5添 ...
- js 替换json对象中的键名
js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...
- win10 家庭中文版打开本地组策略编辑器
win10 家庭中文版打开本地组策略编辑器 CreateTime--2018年5月14日09:01:25 Author:Marydon 1.问题描述 2.问题解析 win10家庭版没有访问本地组策 ...