参考网页 http://blog.youkuaiyun.com/jackystudio/article/details/11757175
源码下载地址:点击打开链接
关于svn的简单使用:点击打开链接
在这里我们主要要搞清楚Cocos2d-x帧动画的实现
参考blog已经提炼的很好了,这里copy一下其过程吧
帧动画的使用步骤:
(1) create,创建CCAnimation类实例。
(2) setDelayPerUnit,设置帧间间隔时间。
(3) addSpriteFrame,添加帧图片。
(4) create,创建CCAnimate类实例,传入(1)的CCAnimation实例。注意:CCAnimation是动画过程,CCAnimate才是动画动作。
(5) 精灵调用runAction即可产生动画效果。
(6) CCAnimationCache这是一个动画类全局缓冲池。(本例子中没有用到)
PlaneLayer类的实现
//PlaneLayer.h
#ifndef __PLANELAYER_H__
#define __PLANELAYER_H__
#include "cocos2d.h"
USING_NS_CC;
const int AIRPLANE=747;
class PlaneLayer : public CCLayer
{
public:
PlaneLayer(void);
~PlaneLayer(void);
static PlaneLayer *create();
static PlaneLayer *sharedPlane;
virtual bool init();
};
#endif
//PlaneLayer.cpp
#include "PlaneLayer.h"
PlaneLayer *PlaneLayer::sharedPlane = NULL;
PlaneLayer::PlaneLayer(void)
{
}
PlaneLayer::~PlaneLayer(void)
{
}
PlaneLayer *PlaneLayer::create()
{
PlaneLayer *pRet = new PlaneLayer();
if(pRet && pRet->init())
{
pRet->autorelease();
sharedPlane = pRet;
return pRet;
}
else
{
CC_SAFE_DELETE(pRet);
return NULL;
}
}
bool PlaneLayer::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui/shoot.plist");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *plane = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));
plane->setPosition(ccp(winSize.width / 2, plane->getContentSize().height / 2));
this->addChild(plane, 0, AIRPLANE);
CCBlink *blink = CCBlink::create(1, 3);
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.1f);
animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));
animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero2.png"));
CCAnimate *animate = CCAnimate::create(animation);
plane->runAction(blink);
plane->runAction(CCRepeatForever::create(animate));
bRet = true;
}while(0);
return bRet;
}
将上面的文件添加进项目工程
在GameLayer.h中添加PlaneLayer.h头文件 类中添加私有成员 PlaneLayer* planeLayer;
在GameLayer.cpp中构造时初始化 planeLayer = NULL; 函数 init 时添加PlaneLayer的类
即加入 this->planeLayer=PlaneLayer::create(); this->addChild(planeLayer); 2语句
编译运行工程 则可看到飞机好像一直再向上飞了,飞机本身也有喷气的动画,效果图如下: