Cocos2d-x 与Cocosbuilder结合使用的一些技巧

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 与Cocosbuilder结合使用的一些技巧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://jetion.diandian.com/post/2012-12-04/40047798280,感谢作者分享

1. 使用Cocosbuilder制作帧动画,Publish的时候崩溃了。

解决方法如下:File -> projectsetting -> 取消选择 flatten paths when publishing

2. 使用Cocosbuilder制作完骨骼动画后,和Cocos2d-x中的Action结合,发现action只运行极短的时间就停止了。

解决方法

其实这是因为在Cocosbuilder中设置了loop动画,在CCBAnimationManager中,当一次动画执行完毕后,它会执行stopAllAction(),而这个时候,我们的action还没有执行完毕,如果你的action的时间 > 运行一次Cocosbuilder动画的时间,就会出现这种情况。这时候我们只要稍微改一下原CCBAnimationManager中的runAniamtions()代码即可,如下:

void CCBAnimationManager::runAnimations(int nSeqId,float fTweenDuration)
{
    CCAssert(nSeqId != -1,"Sequence id couldn't be found");
                                                 
                                             
    mRootNode->stopActionByTag(100);
                                                 
    CCDictElement* pElement = NULL;
    CCDICT_FOREACH(mNodeSequences,pElement)
    {
        CCNode *node = (CCNode*)pElement->getIntKey();
        node->stopAllActions();
                                                     
        // Refer to CCBReader::readKeyframe() for the real type of value
        CCDictionary *seqs = (CCDictionary*)pElement->getObject();
        CCDictionary *seqNodeProps = (CCDictionary*)seqs->objectForKey(nSeqId);
                                                     
        set<string> seqNodePropNames;
                                                     
        if (seqNodeProps)
        {
            // Reset nodes that have sequence node properties,and run actions on them
            CCDictElement* pElement1 = NULL;
            CCDICT_FOREACH(seqNodeProps,pElement1)
            {
                const char *propName = pElement1->getStrKey();
                CCBSequenceProperty *seqProp = (CCBSequenceProperty*)seqNodeProps->objectForKey(propName);
                seqNodePropNames.insert(propName);
                                                             
                setFirstFrame(node,seqProp,fTweenDuration);
                runAction(node,fTweenDuration);
            }
        }
                                                     
        // Reset the nodes that may have been changed by other timelines
        CCDictionary *nodeBaseValues = (CCDictionary*)mBaseValues->objectForKey(pElement->getIntKey());
        if (nodeBaseValues)
        {
            CCDictElement* pElement2 = NULL;
            CCDICT_FOREACH(nodeBaseValues,pElement2)
            {
                if (seqNodePropNames.find(pElement2->getStrKey()) == seqNodePropNames.end())
                {
                    CCObject *value = pElement2->getObject();
                                                                 
                    if (value)
                    {
                       setAnimatedProperty(pElement2->getStrKey(),node,value,fTweenDuration);
                    }
                }
            }
        }
    }
                                                 
    // Make callback at end of sequence
    CCBSequence *seq = getSequence(nSeqId);
    CCAction *completeAction = CCSequence::createWithTwoActions(CCDelayTime::create(seq->getDuration() + fTweenDuration),CCCallFunc::create(this,callfunc_selector(CCBAnimationManager::sequenceCompleted)));
    completeAction->setTag(100);
    mRootNode->runAction(completeAction);
                                                 
    // Set the running scene
    mRunningSequence = getSequence(nSeqId);
                                                 
}

第6行和第59行为添加代码

3. 给精灵添加发光的效果

在CCSprite右边的编辑窗口有一个Blend的设置项,我们只要选择下面的Additive即可实现发光效果; 手动设置Blend src为one- dst color,Blend dst为one, 可以去掉精灵的Alpha通道,这在去除某些黑色背景图片有不错的效果

4.使用Cocosbuilder中的Visible动画时报错

这里要注意,是动画中的Visible,然后设置该动画为AutoRun,运行它就报错,报错内容如下:


但是如果你没设置AutoRun,而是手动的触发它,并不会报错。

暂时不知道为什么会出现这种情况,你可以灵活的使用opacity来代替显示隐藏的功能

未完待续。。。


另外篇博客

这样可以解决根节点的动画与2dx的action同时运作,但是无法解决子节点的问题。

完整的修改方案还需要增加这个部分的修改

  1. voidCCBAnimationManager::runAction(CCNode*pNode,CCBSequenceProperty*pSeqProp,floatfTweenDuration)
  2. {
  3. CCArray*keyframes=pSeqProp->getKeyframes();
  4. intnumKeyframes=keyframes->count();
  5. if(numKeyframes>1)
  6. {
  7. //Makeananimation!
  8. CCArray*actions=CCArray::create();
  9. CCBKeyframe*keyframeFirst=(CCBKeyframe*)keyframes->objectAtIndex(0);
  10. floattimeFirst=keyframeFirst->getTime()+fTweenDuration;
  11. if(timeFirst>0)
  12. actions->addObject(CCDelayTime::create(timeFirst));
  13. }
  14. for(inti=0;i<numKeyframes-1;++i)
  15. CCBKeyframe*kf0=(CCBKeyframe*)keyframes->objectAtIndex(i);
  16. CCBKeyframe*kf1=(CCBKeyframe*)keyframes->objectAtIndex(i+1);
  17. CCActionInterval*action=getAction(kf0,kf1,pSeqProp->getName(),pNode);
  18. if(action)
  19. //Applyeasing
  20. action=getEaseAction(action,kf0->getEasingType(),kf0->getEasingOpt());
  21. actions->addObject(action);
  22. }
  23. CCFiniteTimeAction*seq=CCSequence::create(actions);
  24. //修改
  25. constintchildActionTag=200;
  26. seq->setTag(childActionTag);
  27. pNode->runAction(seq);
  28. }

以及

copy

    voidCCBAnimationManager::runAnimationsForSequenceIdTweenDuration(intnSeqId,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> CCAssert(nSeqId!=-1,"Sequenceidcouldn'tbefound");
  1. //修改部分
  2. intcurrentActionTag=1000;
  3. mRootNode->stopActionByTag(currentActionTag);
  4. //mRootNode->stopAllActions();
  5. CCDictElement*pElement=NULL;
  6. CCDICT_FOREACH(mNodeSequences,pElement)
  7. CCNode*node=(CCNode*)pElement->getIntKey();
  8. //修改部分
  9. node->stopActionByTag(childActionTag);
  10. //node->stopAllActions();
  11. //RefertoCCBReader::readKeyframe()fortherealtypeofvalue
  12. CCDictionary*seqs=(CCDictionary*)pElement->getObject();
  13. CCDictionary*seqNodeProps=(CCDictionary*)seqs->objectForKey(nSeqId);
  14. set<string>seqNodePropNames;
  15. if(seqNodeProps)
  16. //Resetnodesthathavesequencenodeproperties,andrunactionsonthem
  17. CCDictElement*pElement1=NULL;
  18. CCDICT_FOREACH(seqNodeProps,pElement1)
  19. char*propName=pElement1->getStrKey();
  20. CCBSequenceProperty*seqProp=(CCBSequenceProperty*)seqNodeProps->objectForKey(propName);
  21. seqNodePropNames.insert(propName);
  22. setFirstFrame(node,fTweenDuration);
  23. runAction(node,fTweenDuration);
  24. //Resetthenodesthatmayhavebeenchangedbyothertimelines
  25. CCDictionary*nodeBaseValues=(CCDictionary*)mBaseValues->objectForKey(pElement->getIntKey());
  26. if(nodeBaseValues)
  27. CCDictElement*pElement2=NULL;
  28. CCDICT_FOREACH(nodeBaseValues,pElement2)
  29. if(seqNodePropNames.find(pElement2->getStrKey())==seqNodePropNames.end())
  30. CCObject*value=pElement2->getObject();
  31. if(value)
  32. setAnimatedProperty(pElement2->getStrKey(),0); background-color:inherit">//Makecallbackatendofsequence
  33. CCBSequence*seq=getSequence(nSeqId);
  34. CCAction*completeAction=CCSequence::createWithTwoActions(CCDelayTime::create(seq->getDuration()+fTweenDuration),
  35. CCCallFunc::create(this,callfunc_selector(CCBAnimationManager::sequenceCompleted)));
  36. completeAction->setTag(currentActionTag);
  37. mRootNode->runAction(completeAction);
  38. //Settherunningscene
  39. if(seq->getCallbackChannel()!=NULL){
  40. CCAction*action=(CCAction*)actionForCallbackChannel(seq->getCallbackChannel());
  41. if(action!=NULL){
  42. mRootNode->runAction(action);
  43. if(seq->getSoundChannel()!=NULL){
  44. CCAction*action=(CCAction*)actionForSoundChannel(seq->getSoundChannel());
  45. if(action!=NULL){
  46. mRootNode->runAction(action);
  47. mRunningSequence=getSequence(nSeqId);
  48. }
原文链接:https://www.f2er.com/cocos2dx/346634.html

猜你在找的Cocos2d-x相关文章