CCCallBlockN's alternative in Cocos2d v3.0

时光总嘲笑我的痴心妄想 提交于 2019-12-14 03:12:56

问题


What's CCCallBlockN alternative for Cocos2d 3.0 ?

Here is my Cocos2d 2.0 Code:

    id calBlock = [CCCallBlockN actionWithBlock:^(CCNode *node){
        node.position         = orign;
    }];

回答1:


The CCCallBlockN and CCCallBlockND variants have always been superfluous since blocks can reference objects in the local scope:

id someData = (some data however created or obtained);
CCNode* someNode = (some node however created or obtained);
id callBlock = [CCActionCallBlock actionWithBlock:^{
    someNode.position = origin;
    [someData quickDoSomething];
}];
[someNode runAction:callBlock];

You just need to have a reference like someNode in the outer scope of the block in order to use it inside the block.

You will usually have the desired node reference because after all you're going to run the action on the desired node after creating the action. Only in cases where you create the actions first and run them later would the passed-in node be useful, but I guess that's a rare situation and probably not good style anyway.



来源:https://stackoverflow.com/questions/22038737/cccallblockns-alternative-in-cocos2d-v3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!