您的位置:首页 > 移动开发 > Cocos引擎

使用CCAnimate、CCAnimation、CCTextureCache、CCTexture2D来实现动画效果

2014-08-22 15:35 459 查看
使用CCTexture2D来创建动画效果,前提资源是有一张合成的大图

下面看具体的做法:

CCSprite* heroSprite = CCSprite::create();

heroSprite -> setAnchorPoint(ccp(0.35,0.3));

heroSprite -> setPosition(ccp(heroSprite -> getContentSize().width/2, m_winSize.height/2));

this -> addCHild(heroSprite,1,0);

CCAnimation* animation = CCAnimation::create();

animation -> setDelayPerUnit(0.3f);//设定时间间隔

animation -> setLoops(-1);//(-1)表示一直执行

CCTextureCache* cache = CCTextrueCache::sharedTextureCache();

CCTexture2D* texture = cache -> addImage("background.png");//这张图片上有要实现运动的各个图片

for(int i = 0; i < 5; i++)

{

  CCRect rect = CCRectMake(85*i,0,85,121);

  animation -> addSpriteFrameWithTexture(texture,rect);

}

CCAnimate* animate = CCAnmiate::create(animation);
heroSprite -> runAction(animate);

当然,如果你的图片是自己合成的,就应该有plist文件,这时候,就不用使用CCTexture2D来实现动画效果了,还有一个更简单的方法

例如:

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();

cache -> addSpriteFrameWithFile("background.plist");

char temp[20];

CCArray* plistArray = CCArray::createWithCapacity(10);

for(int i = 0; i < 10; i++)

{

  sprintf(temp,"pook%d",i);

  CCSpriteFrame* frame = cache -> spriteFrameByName(temp);

  plistArray -> addObject(frame);

}

CCAnimation* animation = CCAnimation::createWithSpriteFrames(plistArray,0.1);

CCAnimate* animate = CCAnimate::create(animation);

//然后让执行此动画的精灵开始执行animate即可

//heroSprite -> runAction(animte);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息