cocos2d-x 动画CCAnimation

       http://download.csdn.net/detail/wu_123_456/7615263

1.在init()函数中添加如下代码:

CCSprite* sprite2 = CCSprite::create("animation.png",CCRectMake(1,1,200,285));

sprite2->setPosition(ccp(200,150));
this->addChild(sprite2,4);
sprite2->runAction(createAnimation());

 

CCRectMake功能是在该图片资源中截取某一部分。

 

2.createAnimation()函数的创建

CCAction* NodeHelloWorld::createAnimation()
{
	CCSpriteBatchNode *bathnode = CCSpriteBatchNode::create("animation.png");
	int iFrameNum = 4;
	CCSpriteFrame* frame = NULL;

	CCArray* frameArray = CCArray::create();

	for (int i = 0; i < iFrameNum ;++i)
	{
		int x = i*200+1;
		int y = 1;
		
		
		frame = CCSpriteFrame::createWithTexture(bathnode->getTexture(),CCRectMake(x,y,200,285));
		
		frameArray->addObject(frame);
	}

	CCAnimation* animation = CCAnimation::createWithSpriteFrames(frameArray);
	animation->setLoops(-1);
	animation->setDelayPerUnit(0.5f);

	CCAction* action = CCAnimate::create(animation);

	return action;
}


创建动画,过程中使用CCSpriteFrame帧控制,再由帧创建出CCAnimation(名词),要想动起来,还得创建动作(也就是动词)CCAnimate,对于内存中的优化,在上述代码中使用的是CCSpritebatchNode,也可使用精灵帧缓存类CCSpriteFrameCache,如上一篇中所描述,要有plist文件。

参考代码:

CCAction* TollgateScene::createAnimation()
{
	CCSpriteFrameCache *boysCache = CCSpriteFrameCache::sharedSpriteFrameCache();
	boysCache->addSpriteFramesWithFile("boys.plist","boys.png");

	CCSpriteFrame *frame = NULL;
	CCArray* framearray = CCArray::create();

	for (int i = 1; i <= MAX_BOYS_COUNTS;++i)
	{
		CCString *str = CCString::createWithFormat("run%d.png",i);
		
		frame = boysCache->spriteFrameByName(str->getCString());

		framearray->addObject(frame);
	}
	
	CCAnimation*  animation = CCAnimation::createWithSpriteFrames(framearray);
	animation->setLoops(-1);
	animation->setDelayPerUnit(0.03f);

	CCAction* action = CCAnimate::create(animation);

	return action;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值