---
title: "CCAnimationCache动画使用"
author: "rayln"
date: "2013-12-03T21:06:52Z"
tags: []
categories: ["移动开发"]
slug: "ccanimationcache"
draft: false
lastmod: "2013-12-16T11:50:10Z"
id: 1984504
url: "https://rayln.iteye.com/blog/1984504"
---

**存储动画**


```c++

CCSpriteFame *sprite1 = CCSpriteFrame::create("/helloworld1.png", CCRectMake(0,0,100,100));
CCSpriteFame *sprite2 = CCSpriteFrame::create("/helloworld2.png", CCRectMake(0,0,100,100));

CCArray *frame = CCArray::create(sprite1,sprite2,NULL);
CCAnimation *animation = CCAnimation::create(frame,1.0f);
CCAnimationCache::shareAnimationCache()->addAnimation(animation, "moveUp");

```


**读取动画**

```c++

CCAnimation *moveUp = CCAnimationCache::shareAnimationCache()->animationByName("moveUp");

CCSprite *sprite = CCSprite::create();
this->addChild(sprite);
sprite->runAction(CCRepeatForever::create(CCAnimate::create(moveUp)));

```


**记得先让精灵停止当前动画再执行新的动画**

```c++

sprite->stopAllActions();

```



**完整的CODE**

```c++

CCArray *arr = CCArray::create();
	for (int i = 1; i < 5; i++)
	{
		CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("image 29%d.png", i)->getCString());
		arr->addObject(frame);
	}
	CCAnimation *anim = CCAnimation::createWithSpriteFrames(arr, .1f);
	CCAnimationCache::sharedAnimationCache()->addAnimation(anim, "walk");

```