Cocos2d-x初学指南(4): 生成animation 帧动画的2种方法

来源:未知 责任编辑:智问网络 发表时间:2013-09-02 11:48 点击:

方法一是用多套名字按一定规律的不同的单图png,

[cpp] 
        //创建cache 
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); 
char strPlist[64] = {0}; 
char strPng[64] = {0}; 
sprintf(strPlist,"fei.plist"); 
//sprintf(strPng,"fei.pvr.ccz");  
sprintf(strPng,"fei.png"); 
cache->addSpriteFramesWithFile(strPlist, strPng); 
 
//创建动画每一帧,从cache里面读取 
CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(6); 
 
char str[64] = {0}; 
for(int i = 1; i <= 6; i++)  

    sprintf(str, "飞%04d.png", i); 
    CCSpriteFrame* frame = cache->spriteFrameByName( str ); 
    animFrames->addObject(frame); 

 
CCAnimation* animation = CCAnimation::animationWithFrames(animFrames,0.04f); 
 
CCRepeatForever* mFly=CCRepeatForever::actionWithAction( CCAnimate::actionWithAnimation(animation, false)); 
animFrames->release(); 
cache->removeSpriteFramesFromFile(strPlist); 
这个代码是网上随便百度的。http://blog.csdn.net/yanghuiliu/article/details/6933421

还有一种方法是多个图在一张合图上,这种更节约磁盘空间,我感觉哈。

 

[cpp] 
//组合图 
        CCTexture2D *birdTexture=CCTextureCache::sharedTextureCache()->addImage("bird2.png"); 
        CCMutableArray<CCSpriteFrame*>* myArray=new CCMutableArray<CCSpriteFrame*>(3); 
        CCSpriteFrame *myframe1=CCSpriteFrame::frameWithTexture(birdTexture,CCRect(1,379,31,30)); 
        CCSpriteFrame *myframe2=CCSpriteFrame::frameWithTexture(birdTexture,CCRect(1,411,31,30)); 
        CCSpriteFrame *myframe3=CCSpriteFrame::frameWithTexture(birdTexture,CCRect(1,508,30,28)); 
        CCSpriteFrame *myframe4=CCSpriteFrame::frameWithTexture(birdTexture,CCRect(1,538,30,29)); 
        myArray->addObject(myframe1); www.2cto.com
        myArray->addObject(myframe2); 
        myArray->addObject(myframe3); 
        myArray->addObject(myframe4); 
        CCAnimation *animation=CCAnimation::animationWithFrames(myArray,1.0f); 
        CCRepeatForever *myaction=CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation,false)); 
        CCSprite *mybird=CCSprite::spriteWithSpriteFrame(myframe1); 
        this->addChild(mybird); 
        mybird->setPosition(ccp(200,300)); 
        mybird->runAction(myaction); 
        myArray->release(); 


 

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • cocos2d-x学习笔记(19)--label 、label atlas
    • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
    • Cocos2d-x学习(一):HelloWorld
    • cocos2dx在xcode下开发,编译到android上(2)
    • cocos2d 设置屏幕默认方向
    • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
    • Cocos2d-x 2.0 之 Actions “三板斧” 之一
    • cocos2d-x学习笔记(18)--游戏打包(windows平台)
    • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1