cocosbuilder ccb中的元素到 lua 对象的转换对照


首先引用cocos2dx库提供的 CCBReaderLoad

  require "CCBReaderLoad"



定义一个加载函数 指定需要加载得ccbi

function LoadSceneFromCocosBuilder()


    local  proxy = CCBProxy:create()

    local  node  = CCBReaderLoad("MainScene.ccbi",proxy,true,"MainScene")

    local  layer = tolua.cast(node,"CCLayer")


    -- MainScene = layer;

    local scene = CCScene:create();

    scene:addChild(layer)

    cclog("load complete")

    return scene

end



然后写一个main函数(默认执行的入口)

local function main()


配置一些系统参数

    -- avoid memory leak

    collectgarbage("setpause", 100)

    collectgarbage("setstepmul", 5000)


这一行的作用是将加载后主场景与 MainScene 对应一起,所以这里ccb中的参数要和ccb中根节点的JS Controller的名称相同

 而且注意统一得选用doc root var

   


MainScene = MainScene or {}

ccb["MainScene"] = MainScene  


调用前面定义的加载函数拿到主场景

   local scene  = LoadSceneFromCocosBuilder()


    if nil ~= scene then

        CCDirector:sharedDirector():pushScene(CCTransitionFade:create(0.5, scene, ccc3(0,0,0))); 

    end


push过去,上面这句执行完毕后,就可以用过MainScene.xxxx来调用自定义的一些子节点对象了,如

 

    local ccLabelTTF = tolua.cast(MainScene.helloLabel,"CCNode")

    ccLabelTTF:setString("now ,i have got label");

就可以拿到label,对其赋值

   

定义点击事件

同时还可以定义button,并且设置其点击函数为onClick

    

MainScene.onClick = function() -- 这里的onClickClickMe按钮的回调函数

        cclog("clicked Button");

        local ccLabelTTF = tolua.cast(MainScene.helloLabel,"CCNode")

        ccLabelTTF:setVisible(true)


        if nil ~= MainScene["mAnimationManager"] then

        local animationMgr = tolua.cast(MainScene["mAnimationManager"],"CCBAnimationManager")

       if nil ~= animationMgr then

      animationMgr:runAnimationsForSequenceNamedTweenDuration("mainloop", 0)  

        end

    end

end

  这样就指定了点击button后,跑一次mainloop







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值