功夫小子实践开发-基本工具类的分析和实现

基本的工具类分析和实现

由于我们的游戏内容和属性决定我们游戏中的主角和游戏中怪物会有各种动作,这些动作的创建我这里采用帧动画的形式,因此对于众多的动作,有一个专门的动画创建器是必须而且有利于减少代码的重复的,下面是关于帧动画(Frame By Frame),其基本的解释是 在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。
比如下面主角跑动作的动画帧:

需要注意的是这些动画帧的命名规律。
关于帧动画:

这里的帧缓存是指我们将游戏的图片帧资源进行打包预加载进入游戏缓存,这部分会在后面的课程中详细讲解,这里不详细赘述。

下面就是动画创建器的一些关键部分的代码:
头文件:
[cpp]  view plain  copy
 print ?

  1.   
  2. #ifndef ActionTool_H__  
  3. #define ActionTool_H__  
  4.   
  5. #include "cocos2d.h"  
  6. USING_NS_CC;  
  7.   
  8. class ActionTool {  
  9. public:  
  10.     static Animate* animationWithFrameName(const char *frameName, int iloops, float delay);  
  11.     static Animate* animationWithFrameAndNum(const char *frameName, int num, float delay);  
  12. };  
  13.   
  14. #endif // ActionTool_H__s  

Cpp文件

#include "ActionRool.h"

Animate* ActionTool::animationWithFrameName(const char *each_name, int iloops, float delay)
{
	SpriteFrame* frame = NULL;
	Animation* animation= Animation::create();
	int index = 1;
	do
	{
		String *name = String::createWithFormat("%s%d",each_name,index++);
		frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(name->getCString());
		if(frame == NULL)
		{
			break;
		}
		animation->addSpriteFrame(frame);
	}while(true);
	animation->setDelayPerUnit(delay);//帧时间间隔
	animation->setRestoreOriginalFrame(true);//是否播放完毕后返回到原始的一帧
	//@param restoreOriginalFrame Whether to restore the original frame when animation finishes.
	Animate* animate = Animate::create(animation);
	return animate;
}

Animate* ActionTool::animationWithFrameAndNum(const char *frameName, int framecount, float delay)
{
	SpriteFrame* frame = NULL;
	Animation* animation= Animation::create();
	for(int index = 1; index <= framecount; index++)
	{
		String *name = String::createWithFormat("%s%d",frameName,index++);
		frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(name->getCString());
		animation->addSpriteFrame(frame);
	}
	animation->setDelayPerUnit(delay);
	animation->setRestoreOriginalFrame(true);
	Animate* animate = Animate::create(animation);
	return animate;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值