cocos2d-x动画(转)

本文介绍在Cocos2d-x中使用plist文件及直接指定纹理坐标两种方式实现动画的具体步骤。通过加载图片资源、创建精灵帧并设置动画播放速度来实现动画效果。

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

01
    //使用 plist 的动画
02
    CCSpriteFrameCache *sfcache = CCSpriteFrameCache::sharedSpriteFrameCache();
03
    sfcache->addSpriteFramesWithFile("yy/yy.plist");
04
 
05
    CCMutableArray *anim_frames_go = new CCMutableArray();
06
    char tmp[50];
07
    for (int j = 0; j < 5; j++)
08
    {
09
        sprintf(tmp, "yy_%d.png", j);
10
        CCSpriteFrame *frame = sfcache->spriteFrameByName(tmp);
11
        anim_frames_go->addObject(frame);
12
    }
13
    CCAnimation *animation_go = CCAnimation::animationWithFrames(anim_frames_go, 0.2f);
14
    anim_frames_go->release();
15
    CCAnimate animate_go = CCAnimate::actionWithAnimation(animation_go, true);
16
    sprite->runAction(CCRepeatForever::actionWithAction(animate_go));
17
 
18
 
19
    //不使用 plist 的
20
    CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("yy/yy.png");
21
    CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*0, 112, 64));
22
    CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*1, 64*0, 112, 64));
23
    CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*1, 112, 64));
24
    CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*1, 64*1, 112, 64));
25
    CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*2, 112, 64));
26
 
27
    CCMutableArray *anim_frames_go = new CCMutableArray(5);
28
    anim_frames_go->addObject(frame0);
29
    anim_frames_go->addObject(frame1);
30
    anim_frames_go->addObject(frame2);
31
    anim_frames_go->addObject(frame3);
32
    anim_frames_go->addObject(frame4);
33
 
34
    CCAnimation *animation_go = CCAnimation::animationWithFrames(anim_frames_go, 0.2f);
35
    anim_frames_go->release();
36
 
37
    CCAnimate *animate_go = CCAnimate::actionWithAnimation(animation_go, true);
38
    msprite->setDisplayFrame(frame0);
39
    sprite->runAction(CCRepeatForever::actionWithAction(animate_go));

原文地址: http://my.oschina.net/yunyu/blog/62862
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值