这是本人的课堂笔记,刚刚开始学习cocos2dx,所以请各位大神不要在意笔记质量。。。
=====================================.h================================
#include "cocos2d.h"
USING_NS_CC;
class xingzou2:public CCLayer
{
public:
CCSpriteBatchNode *node;
virtual bool init();
static CCScene *scene();
void zhen();
CREATE_FUNC(xingzou2);
};
=====================================.h================================
#include "xingzou2.h"
bool xingzou2::init()
{
if(CCLayer::init())
{
//叫来ccspriteframecache内存池
CCSpriteFrameCache *cache =CCSpriteFrameCache::sharedSpriteFrameCache();
//将plist文件添加进入内存池
cache->addSpriteFramesWithFile("yaoling1.plist");
//
this->node = CCSpriteBatchNode::create("yaoling1.png");
//将node层放入layer层中
this->addChild(node);
this->zhen();
return true;
}
return false;
}
void xingzou2::zhen()
{
//叫来ccspriteframecache内存池
CCSpriteFrameCache *cache= CCSpriteFrameCache::sharedSpriteFrameCache();
//new一个数组
CCArray *arr= CCArray::create();
//循环
for (int i=0; i<6; i++)
{
//将名字和图片互相匹配
sprintf(name, "000%d.png",i);
//通过名称从内存池中取出数据
CCSpriteFrame *frame = cache->spriteFrameByName(name);
//将数据存入数组中
arr->addObject(frame);
}
//创建动画序列帧
CCAnimation *action=CCAnimation::createWithSpriteFrames(arr);
//设置序列帧的播放间隙
action->setDelayPerUnit(0.3);
//设置序列帧的循环次数
action->setLoops(-1);
//使用序列帧来创建动画播放
CCAnimate *animate=CCAnimate::create(action);
//使用图片名称来new一个精灵
CCSprite *sprite = CCSprite::createWithSpriteFrameName(name);
//运行动画播放动作
sprite ->runAction(animate);
//设置比例缩放
sprite ->setScale(3);
//将精灵放入node层中
this->node->addChild(sprite);
//设置精灵在屏幕中的坐标位置
sprite->setPosition(ccp(200, 300));
}
CCScene* xingzou2::scene()
{
CCScene *sc=CCScene::create();
xingzou2 *xing=xingzou2::create();
sc->addChild(xing);
return sc;
}