cocos2d-x lua 回调函数

来源:互联网 发布:淘宝店网页制作 编辑:程序博客网 时间:2024/06/06 18:14

在使用cocos2d-x 的回调函数的时候,遇到了些问题也是就上网查找了例子,发现了一篇不错的博文。

原文地址:http://blog.csdn.net/star530/article/details/21245565

微博已经将回调函数分析得比较透彻了,不过还是有些疑问就是多个参数的回调。

在cocos2d 3.x 中回调函数只有两个了分别是CallFunc()与CallFuncN(),博文中只使用了CallFunc()

我借用了下代码,并添加了CallFuncN(),对比俩个回调函数的差别。

    //先是创建3个精灵    boy = Sprite::create("boy.png");//创建boy    boy->setPosition(Point(visibleSize.width/2,visibleSize.height/2));    this->addChild(boy,1);        girl_1 = Sprite::create("girl_1.png");//创建girl1    girl_1->setPosition(Point(visibleSize.width/3,visibleSize.height/2));    girl_1->setTag(10);    this->addChild(girl_1,1);        girl_2 = Sprite::create("girl_2.png");//创建girl2    girl_2->setPosition(Point(2*visibleSize.width/3,visibleSize.height/2));    girl_2->setTag(20);    this->addChild(girl_2,1);        //让boy运动,通过Callfunc回调到callback1    boy->runAction(CCSequence::create(MoveBy::create(1.0f,Point(0,100)),                                      CallFunc::create(CC_CALLBACK_0(HelloWorld::callback1,this)),//                                      CallFunc::create(std::bind(&HelloWorld::callback1, this)),                                      NULL));                return true;}void HelloWorld::callback1(){CCLOG("in callback1");//girl1运动,最后回调到callback2girl_1->runAction(CCSequence::create(MoveBy::create(1.0f,Point(0,150)),//                                         CallFunc::create(CC_CALLBACK_0(HelloWorld::callback2,this,girl_1)),//                                         CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback2,this)),                                         CallFunc::create(std::bind(&HelloWorld::callback2,this,girl_1)),//                                         CallFuncN::create(std::bind(&HelloWorld::callback2,this,girl_1)),                                         NULL));}void HelloWorld::callback2(Node* sender){//girl2运动,最后回调到callback3girl_2->runAction(CCSequence::create(MoveBy::create(1.0f,Point(0,200)),//                                         CallFunc::create(CC_CALLBACK_0(HelloWorld::callback3,this,girl_2,99)),                                         CallFuncN::create(CC_CALLBACK_1(HelloWorld::callback3,this,99)),                                         CallFunc::create(std::bind(&HelloWorld::callback3,this,girl_1,99)),                                         CallFuncN::create(std::bind(&HelloWorld::callback3,this,girl_1,99)),                                         NULL));    CCLOG("in callback2,sender tag is:%d",(Sprite*)sender->getTag());}void HelloWorld::callback3(Node* sender, long data){//最终输出CCLOG("in callback3,everything is OK,sender tag is:%d,date is:%ld",(Sprite*)sender->getTag(),data);CCLOG("girl2 dandan ask:what fake the CC_CALLBACK is?");}

出于学习lua,就随便写了个lua版的

require "Cocos2d"require "Cocos2dConstants"-- cclogcclog = function(...)    print(string.format(...))end-- for CCLuaEngine tracebackfunction __G__TRACKBACK__(msg)    cclog("----------------------------------------")    cclog("LUA ERROR: " .. tostring(msg) .. "\n")    cclog(debug.traceback())    cclog("----------------------------------------")    return msgendfunction main(  )    local visibleSize = cc.Director:getInstance():getVisibleSize()local sceneGame = cc.Scene:create()    boy = cc.Sprite:create("boy.png")    boy:setPosition(visibleSize.width/2,visibleSize.height/2)    sceneGame:addChild(boy,1)    girl_1   = cc.Sprite:create("girl_1.png")    girl_1:setPosition(visibleSize.width/3,visibleSize.height/2)    girl_1:setTag(10)    sceneGame:addChild(girl_1,1)    girl_2 = cc.Sprite:create("girl_2.png")    girl_2:setPosition(visibleSize.width/3*2,visibleSize.height/2)        girl_2:setTag(20)    sceneGame:addChild(girl_2,1)    boy:runAction(cc.Sequence:create(cc.MoveBy:create(1.0,cc.p(0,100)),cc.CallFunc:create(backcall1)))if cc.Director:getInstance():getRunningScene() thencc.Director:getInstance():replaceScene(sceneGame)elsecc.Director:getInstance():runWithScene(sceneGame)endendfunction backcall1(  )    print("backcall1")    girl_1:runAction(cc.Sequence:create(cc.MoveBy:create(1.0,cc.p(0,150)),cc.CallFunc:create(backcall2)))    print("what")endfunction backcall2( a )    print("backcall2")    print(a:getTag())    girl_2:runAction(cc.Sequence:create(cc.MoveBy:create(1.0,cc.p(0,200)),cc.CallFunc:create( function(  )       backcall3 (girl_2,3)    end  )))endfunction backcall3( b, i )    print("backcall3")    print(b:getTag())    print(i)endlocal status, msg = xpcall(main, __G__TRACKBACK__)if not status then    error(msg)end

由于不知道lua的多参数回调怎么传值,所以用了个笨方法,如果有知道的,请指点下小弟。

0 0
原创粉丝点击