cocos2d-x 菜鸟实习生学习篇(七) 逐帧动画

有段时间没更新博客了,这几天都比较忙,要完成boss布置的一些任务,前些天又请了四天假回校考试跟入党转正。对于实习生来说,一下子请四天假堪称胆大包天!话说入党这事真的很纠结,也不知道自己大一点时候怎么就那么傻要申请入党呢?现在有点后悔了。愤青伤不起。

这篇博客我就不参考其他高手的啦,可以看出自己这阶段确实是有点进步的!不过推荐大家看篇程序员“小说”吧。昨天无意间看到的,有一百多篇,挺喜欢的,觉得跟自己找工作时的情况很像。我想,你看了之后肯定也能从其中找到共鸣的!

http://blog.csdn.net/norains?viewmode=contents 那些年,哪些是——一个程序员的奋斗史


好,开始进入正题。

说到这个逐帧动画,额,应该是叫逐帧动画吧。对这货理解的比较容易,因为大一的时候有用半年时间自学过flash,所以用起来也还算顺。

首先从官方自带的tsetcpp说起:

在spritetest中"Sprite offset + anchor + rot";这个例子:有如下几句:

//这是第一种方法,我用的也是这种

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("animations/grossini.plist");
cache->addSpriteFramesWithFile("animations/grossini_gray.plist", "animations/grossini_gray.png");

CCArray* animFrames = CCArray::createWithCapacity(14);
char str[100] = {0};
for(int i = 0; i < 14; i++)
{
sprintf(str, "grossini_dance_%02d.png",(i+1));
CCSpriteFrame *frame = cache->spriteFrameByName(str);
animFrames->addObject(frame);
}

CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.3f);
sprite->runAction(CCRepeatForever::create( CCAnimate::create(animation) ) );


//
// File animation,这是第二种方法
//
// With 2 loops and reverse
CCAnimationCache *cache = CCAnimationCache::sharedAnimationCache();
cache->addAnimationsWithFile("animations/animations-2.plist");
CCAnimation *animation2 = cache->animationByName("dance_1");

CCAnimate* action2 = CCAnimate::create(animation2);
m_tamara->runAction(CCSequence::create(action2, action2->reverse(), NULL));

//
// File animation,这是第三种方法,其实也不能这么说,它就是在第二种的基础上做了一些更基础的改动
//
// with 4 loops
CCAnimation *animation3 = (CCAnimation *)animation2->copy()->autorelease();
animation3->setLoops(4);

CCAnimate* action3 = CCAnimate::create(animation3);
m_kathia->runAction(action3);


好了,test就这么些,但是我必须强调的是:上面几种除了第一种,其他方法我用起来都没成功过!!!

第二种一直卡在CCAnimation *animation2 = cache->animationByName("dance_1");这一步,为什么呢?因为 dance_1这不是图片,这是一个动画,我根本就不知道在什么时候,什么地点要做这个动画,所以,扑街。

第三种跟第二种差不多,无解。


其实有一种傻瓜式的方法。前两个星期我做动画的时候依然采取了下面这种傻瓜式逐帧动画。请看大屏幕:

CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("feng/fall.plist");
CCArray *animFrames = new CCArray(3); // 一共有4幀
CCSpriteFrame *frame = cache->spriteFrameByName("fall1.png"); // 加進第1幀
animFrames->addObject(frame);
frame = cache->spriteFrameByName("fall2.png"); // 加進第2幀
animFrames->addObject(frame);
frame = cache->spriteFrameByName("fall3.png"); // 加進第3幀
animFrames->addObject(frame);
//control the fall each frame of move
CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames,0.2f);
CCAnimate* action = CCAnimate::create(animation);
CCSprite* fall = CCSprite::create("feng/fall1.png");
fall->runAction(CCRepeatForever::create(action));
this->addChild(fall,1);
fall->setAnchorPoint(CCPointZero);
fall->setPosition(ccp(215,370));
fall->setScale(0.5f);


方法很傻,但是很有效。如果有高手知道更方便的方法或者知道test的方法应该如何破解,希望能在评论里给出解答。谢谢

如果要让精灵边走动边要有位置移动,可以这样子:

CCFiniteTimeAction *action_dog_wr = CCSpawn::actions(
CCAnimate::create(animation_dog_wr),
CCMoveBy::create(2.0f,ccp(10,-10)),NULL
);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值