cocos2d-x简单动画:处理Texture2D和plist文件处理动画<代码演示>

本文详细介绍了在游戏开发中实现角色动画的多种方法,包括直接创建精灵、使用动画缓存、读取plist文件中的单帧动画以及利用CCAnimation进行动画处理。通过这些方法,开发者可以高效地为游戏角色赋予生动的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*方法一*/

this->setTouchEnabled(true);
    
texture = CCTextureCache::sharedTextureCache()->addImage("player.png");
    
float width =  texture->getContentSize().width/4;
    
float height = texture->getContentSize().height;
    
CCSprite *sprite = CCSprite::createWithTexture(texture,CCRectMake(00, width, height));
    sprite->
setPosition(ccp(size.width/2,size.height/2));
    
this->addChild(sprite);

/*方法二*/

/*从缓存中获取动画*/
    
/*说明: 
     
原理:实在已有的大图种分别占位,而缓存中大图是不会显示的,
          
分别添加时,就是在原来的大图中寻找图片的位置对应放置
     
     1.player.png
中又4张图片;
     2.
使用
         CCSprite *pSprite = CCSprite::create("player.png");
         CCTexture2D *pText2d = CCTextureCache::sharedTextureCache()->addImage("player.png");
          
         
添加的精灵都是大图精灵,一个大图包含所有动作的图片;
     3.
创建时通过计算的出大图中单个小精灵的尺寸获取对应元素;
     4.
创建CCAnimation  *animation = CCAnimation::create();//创建动画
       
用于存储动画帧
     5.
使用循环分别截取大图种包含的分动作
     6.
使用CCAnimate::create(animation)转换为真正的动作
     7.
使用CCRepeatForever::create()让动作无限重复
          
也可使用animation->setLoops(-1);//无限循环
     8.
让精灵执行动作
         pSprite->runAction();
     
     
     */

    
CCSprite *pSprite = CCSprite::create("player.png");
    
CCTexture2D *pText2d = CCTextureCache::sharedTextureCache()->addImage("player.png");
    
//获取大图宽高
    
float w = pSprite->getContentSize().width/4;
    
float h = pSprite->getContentSize().height;
    
CCAnimation  *animation = CCAnimation::create();//创建动画
    animation->
setDelayPerUnit(0.2f);//设置动画每一帧的间隔
//    animation->setLoops(-1);//无限循环
    
for (int i = 0; i<4; i++) {
        animation->
addSpriteFrameWithTexture(pText2d, CCRectMake(i*w,0100, h));
        pSprite = 
CCSprite::createWithTexture(pText2d,CCRectMake(00,10099));
        pSprite->
runAction(CCRepeatForever::create(CCAnimate::create(animation)));
        
    }
    pSprite->
setPosition(ccp(size.width/4size.height/2));
    
this->addChild(pSprite);



/*方法三*/

    /*获取每个单帧的动画图片
     
     
5张飞机的连续动作的图片:player1,player2,player3,player4,player5
     
分别创建帧,然后添加到CCAnimation
     
     */

    
CCSprite *pSprite = CCSprite::create("player1.png");
    
//获取大图宽高
    
float w = pSprite->getContentSize().width;
    
float h = pSprite->getContentSize().height;
    
CCAnimation  *animation = CCAnimation::create();//创建动画
    animation->
setDelayPerUnit(0.2f);//设置动画每一帧的间隔
//    animation->setLoops(-1);//无限循环
    
for (int i = 1; i<6; i++) {
        
CCString *names = CCString::createWithFormat("player%d.png",i);
        
CCSpriteFrame *frame = CCSpriteFrame::create(names->getCString(), CCRectMake(00, w, h));
        animation->
addSpriteFrame(frame);
        pSprite->
runAction(CCRepeatForever::create(CCAnimate::create(animation)));
        
    }
    pSprite->
setPosition(ccp(size.width/4size.height/2));
    
this->addChild(pSprite);
/*方法四*/

/*
      
如何读取plist文件中的图片FlagZombie_default.png单帧图片动画
     1.
先将FlagZombie_default.png文件的
       
大图FlagZombie_default.png添加到精灵;
     2.
FlagZombie_default.plist文件添加到CCSpriteFrameCache
     3.
定义一个数组用于存储动画精灵帧
     4.
定义动画帧对象接收单帧动画
     5.
设置CCAnimation
     6.
CCAnimation 封装到CCAnimate
     7.
pSprite执行动画
     
     */

    
//创建大图精灵
    
CCSprite *pSprite = CCSprite::create("FlagZombie_default.png");
    
//将大图对应的plist文件读取到CCSpriteFrameCache
    
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->
addSpriteFramesWithFile("FlagZombie_default.plist");
    
char temp[100];
    
//定义一个数组存储CCAnimationFrame
    
CCArray *arr = CCArray ::create();
    
//循环添加动画帧
    
for (int i = 1; i <= 12;i++) {
        
//拼接获取plist文件中的单个图片帧
        
sprintf(temp,"FlagZombie%d.png",i);
        
CCSpriteFrame *frame = cache->spriteFrameByName(temp);
/*****************************重要步骤*************************************/
        
//创建动画帧
        
CCAnimationFrame *animationframe = new CCAnimationFrame();
        animationframe ->
initWithSpriteFrame(frame, 1NULL);
        animationframe->
autorelease();
        
//将动画帧存入
        arr->
addObject(animationframe);
    }
    
//将动画帧存入静态动画库中及放置的间隔时间
    
CCAnimation *animation = CCAnimation::create(arr,0.2);
    animation->
setLoops(-1);//设置无限循环
//    animation->setDelayPerUnit(0.2f);//设置每帧动画放置间隔的时间
    pSprite->
runAction(CCAnimate::create(animation));
    pSprite->
setPosition(ccp(size.width/2,size.height/2));
    
this->addChild(pSprite);
    
/*方法五*/

   

/*
      
如何读取plist文件中的图片FlagZombie_default.png单帧图片动画
     1.
先将FlagZombie_default.png文件的
       
大图FlagZombie_default.png添加到精灵;
     2.
FlagZombie_default.plist文件添加到CCSpriteFrameCache
     3.
定义一个数组用于存储动画精灵帧
     4.
定义动画帧对象接收单帧动画
     5.
设置CCAnimation
     6.
CCAnimation 封装到CCAnimate
     7.
pSprite执行动画
     
     */

   //创建大图精灵
    
CCSprite *pSprite = CCSprite::create("FlagZombie_default.png");
    
//将大图对应的plist文件读取到CCSpriteFrameCache
    
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->
addSpriteFramesWithFile("FlagZombie_default.plist");
    
    
CCArray *array = CCArray::create();
    
//循环添加动画帧
    
for (int i = 1; i <= 12;i++) {
        
//拼接获取plist文件中的单个图片帧
        
CCString *name = CCString::createWithFormat("FlagZombie%d.png",i);
        
CCSpriteFrame *frame = cache->spriteFrameByName(name->getCString());
        
CCLog("%.2f",frame->getRect().size.height);

/*****************************重要步骤*************************************/
        
CCAnimationFrame *animationframe = new CCAnimationFrame();
        animationframe ->
initWithSpriteFrame(frame, 1NULL);
        animationframe->
autorelease();
        array->
addObject(animationframe);
    }
    
CCAnimation *animation = CCAnimation::create(array,0.2f);
    pSprite->
setPosition(ccp(size.width/4size.height/2));
     
this->addChild(pSprite);
    pSprite->
runAction(CCRepeatForever::create(CCAnimate::create(animation)));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值