cocos2d-x学习笔记(二)浅入动画教程。

ufolr原创,转载请注明:

转载自ufolr的博客 原文连接:http://blog.csdn.net/ufolr/article/details/7404403


前一篇教程,把项目中道具系统用到的很多东全揉搓在一起,要说明的很多,但东西实在太多,没办法一一道来。

现在我们就单独的来看看coco2d中动画的的基本实现。

利用上一篇教程中“定时炸弹”的动画来说明吧,先上资源图:

然后上代码:

bool Daoju::ZhadanAnimate(float zx, float zy){
	CCLog("Animate");
	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("Zhadan.png");
	//加载资源(图片起点_左上角,图片大小)
	  //读取资源中的每一帧,暂存到fram
	CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*0, 16, 16));
	CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*0, 16, 16));
	CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*1, 16, 16));
	CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*1, 16, 16));
	CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*2, 16, 16));
	CCSpriteFrame *frame5 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*2, 16, 16));
	CCSpriteFrame *frame6 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*3, 16, 16));
	CCSpriteFrame *frame7 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*3, 16, 16));
	//设置初始图片(第一帧)
	CCSprite* spZhadananim = CCSprite::spriteWithSpriteFrame(frame0);
	spZhadananim->setPosition(ccp(zx, zy));
	addChild(spZhadananim, 0, 200);//加到相应图层,设置标签为200,我们在回调函数中会利用标签找到对应的精灵,然后删除动画精灵

	//合成动画-炸弹等待
	CCMutableArray<CCSpriteFrame*> *animZhadanWaitFrames = new CCMutableArray<CCSpriteFrame*>(4);
	//animZhadanWaitFrames->addObject(frame0);
	//animZhadanWaitFrames->addObject(frame1);
	//animZhadanWaitFrames->addObject(frame2);
	//animZhadanWaitFrames->addObject(frame3);
	animZhadanWaitFrames->addObject(frame4);
	animZhadanWaitFrames->addObject(frame5);
	animZhadanWaitFrames->addObject(frame6);
	animZhadanWaitFrames->addObject(frame7);
	//合成动画-炸弹爆炸
	CCMutableArray<CCSpriteFrame*> *animZhadanExciteFrames = new CCMutableArray<CCSpriteFrame*>(4);
	animZhadanExciteFrames->addObject(frame0);
	animZhadanExciteFrames->addObject(frame1);
	animZhadanExciteFrames->addObject(frame2);
	animZhadanExciteFrames->addObject(frame3);

	
	//将4个动画侦生成CCAnimation对象,每0.1秒(float)播放一帧
	//生成炸弹等待动画
	CCAnimation *animationWait  = CCAnimation::animationWithFrames(animZhadanWaitFrames, 0.1f);
	CCAnimate *animateWait = CCAnimate::actionWithAnimation(animationWait, false);//动画模版生成动画

	//生成炸弹激发动画
	CCAnimation *animationExcit  = CCAnimation::animationWithFrames(animZhadanExciteFrames, 0.2f);
	CCAnimate *animateExcit = CCAnimate::actionWithAnimation(animationExcit, false);//此处值为false动画播放完是在最后一帧,为true则返回第一帧

	//创建回调Action
	CCCallFunc *animOverCallBack = CCCallFunc::actionWithTarget(this, callfunc_selector(Daoju::animBombsOverCallBack));
	//依次执行Action
	spZhadananim->runAction(CCSequence::actions(
		CCRepeat::actionWithAction( animateWait, 12),//第一段动画重复12次
		//CCDelayTime::actionWithDuration(0.001f), //暂停瞬间一般情况下不用
		CCRepeat::actionWithAction( animateExcit, 1), //第二段动画重复1次
		animOverCallBack,//回调函数,用于清屏,清除动画,否则动画播放玩后屏幕会残留在屏幕上(对应上面的值可能是最后一帧也可能是第一帧)
		NULL
	));

	animZhadanWaitFrames->release();//释放资源
	animZhadanExciteFrames->release();
	return true;
}

未完待续。。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值