当前位置:编程学习 > wap >>

cocos2d-x 一个plist创建多个帧动画

闲暇之余,和大家讲讲cocos2d-x 创建帧动画吧,

确切的说是从一个plist文件读取创建多个帧动画,

那么首先我们要用工具将多个帧动画生成一个plist文件(手写plist就太易做图了),

且这个plist里面包含了多个帧动画信息,然后我们再到程序中去调用刚生成的plist,

我用的工具是红孩儿工具箱,大家可以去下载用用,方正目前我要用的功能它是都有了。

首先我们将所需的所有的单图片合拼为一张大图(切/拼图栏),然后保存导出.plist 即 PackSpr.plist,

2 动画编辑栏,Root下绑定新图片Image,右键Image,名字命名为die,创建固定帧关键帧动画,这时候是单图片生成,查找图块,

图片起始和图片结束为一个动画,数值是1而后连续值且图片名字后缀也要如此,第一个动画就弄好了保存die.plist,第二个动画也是同样做法,不过图片起始是上一个图片结束+1了再保存为walk.plist,

我们想想,还是多个plist啊,是啊,其实我们还需要一个步骤才能只生成一个plist,那就是我们在生成2个动画文件plist的时候附带还生成了2个ani文件,只要将这2个ani文件合成一个plist即可,我们打开角色编辑一栏,在空白区域右键,创建新角色类,取名为RoleAni,添加新角色,也取名为RoleAni,最后逐个添加动作倒入我们的动画文件ani吧分别为die和walk,好了保存为RoleAni.plist 这时候plist就大功告成了,最后我们在程序中去调用吧

 

[cpp]
CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();   
        frameCache->addSpriteFramesWithFile("PackSpr.plist");   
        // Purge previously loaded animation    
        CCAnimationCache::purgeSharedAnimationCache();   
        CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();   
        // Add an animation to the Cache    
        animCache->addAnimationsWithFile("RoleAni.plist");   
 
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();   
 
        CCAnimation *die = animCache->animationByName("die");   
        die->setRestoreOriginalFrame(true);  
        CCAnimate *animdie = CCAnimate::create(die);   
        CCSequence *seqdie = (CCSequence*)CCSequence::create(animdie, NULL);   
        CCSprite *sprdie = CCSprite::create();   
        CCSpriteFrame *framedie = frameCache->spriteFrameByName("role_10.bmp");  
        sprdie->setDisplayFrame(framedie);   
        sprdie->setPosition(ccp(winSize.width/2-100, winSize.height/2));   
        sprdie->setScale(0.5);   
        addChild(sprdie);   
        // run the animation    
        sprdie->runAction(seqdie); 
 
        CCAnimation *walk = animCache->animationByName("walk");   
        walk->setRestoreOriginalFrame(true);  
        CCAnimate *animwalk = CCAnimate::create(walk);   
        CCSequence *seqwalk = (CCSequence*)CCSequence::create(animwalk, NULL);   
        CCSprite *sprwalk = CCSprite::create();   
        CCSpriteFrame *framewalk = frameCache->spriteFrameByName("role_1.bmp");   
        sprwalk->setDisplayFrame(framewalk);   
        sprwalk->setPosition(ccp(winSize.width/2, winSize.height/2));  
        addChild(sprwalk);   
        // run the animation    
        sprwalk->runAction(seqwalk); 

CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); 
        frameCache->addSpriteFramesWithFile("PackSpr.plist"); 
        // Purge previously loaded animation 
        CCAnimationCache::purgeSharedAnimationCache(); 
  CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache(); 
        // Add an animation to the Cache 
        animCache->addAnimationsWithFile("RoleAni.plist"); 

  CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 

  CCAnimation *die = animCache->animationByName("die"); 
        die->setRestoreOriginalFrame(true);
  CCAnimate *animdie = CCAnimate::create(die); 
        CCSequence *seqdie = (CCSequence*)CCSequence::create(animdie, NULL); 
        CCSprite *sprdie = CCSprite::create(); 
        CCSpriteFrame *framedie = frameCache->spriteFrameByName("role_10.bmp");
        sprdie->setDisplayFrame(framedie); 
        sprdie->setPosition(ccp(winSize.width/2-100, winSize.height/2)); 
        sprdie->setScale(0.5); 
        addChild(sprdie); 
        // run the animation 
        sprdie->runAction(seqdie);

        CCAnimation *walk = animCache->animationByName("walk"); 
        walk->setRestoreOriginalFrame(true);
        CCAnimate *animwalk = CCAnimate::create(walk); 
        CCSequence *seqwalk = (CCSequence*)CCSequence::create(animwalk, NULL); 
        CCSprite *sprwalk = CCSprite::create(); 
        CCSpriteFrame *framewalk = frameCache->spriteFrameByName("role_1.bmp"); 
        sprwalk->setDisplayFrame(framewalk); 
        sprwalk->setPosition(ccp(winSize.width/2, winSize.height/2));
        addChild(sprwalk); 
        // run the animation 
  sprwalk->runAction(seqwalk);
 

 

 

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,