转载请保留此文:
http://blog.youkuaiyun.com/xuzhuang2008/article/details/8934645
关于cocos2d-x使用cocostudio也好 其他的动画编辑器也好 生成的plist文件和png文件
通过一图多帧的方式 进行动画的播放
cocos2d-x没有取得当前帧的方法 我也不想修改源代码
参考网上的一些做法
如 取得texture的name等 都不适合一图多帧
这里是我想出的一个办法 大致的思路是这样的
在将帧放入frames的时候 保存这些帧的坐标
然后 在需要的时候 取得sprite的displayframe的个体Rect 这样 你就有2个rect
遍历对比 通过rect的origin.x和y来比较是否相同
这里是具体的实现片段
动画的播放部分
//动画播放部分
CCRect rects[8];
CCSprite *flowSprite;
CCAnimate *animate;
CCSpriteFrame *frame1;
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("texiao0.plist","texiao0.png");
CCArray*frames=CCArray::create();
char pngName[20];
//动画有多少帧 这里的i的大小就是多少
for (int i=0;i<8; i++) {
sprintf(pngName, "fire\\hotwindg%02d.png",i+1);
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pngName);
CCRect rec1 = frame->getRect();
rects[i] = rec1;
frames->addObject(frame);
}
//生成CCAnimation对象
CCAnimation* animation =CCAnimation::createWithSpriteFrames(frames,0.06f);
CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"aa");
CCAnimationCache * aniCache = CCAnimationCache::sharedAnimationCache();
CCAnimation* normal = aniCache->animationByName("aa");
frames->release();
animate = CCAnimate::create(normal);
CCFiniteTimeAction *seq = CCSequence::create(animate,NULL);
flowSprite = CCSprite::create();
animate->setTag(20);
flowSprite->autorelease();
frame1 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("fire\\hotwindg01.png");
flowSprite->setDisplayFrame(frame1);
flowSprite->setScale(0.3f);
addChild(flowSprite,160);
flowSprite->runAction(CCRepeatForever::create(animate));
flowSprite->setPosition(ccp(200,200));
CCLog("%d",animate->getAnimation()->getFrames()->count());
获取部分
int currentAnimIndex = 0;
const float EPSINON = 0.000001f;
int count = 8;
for(int i = 0; i < count; i++)
{
CCRect rect2 = flowSprite->displayFrame()->getRect();
float y = rect2.origin.y - rects[i].origin.y ;
float x = rect2.origin.x - rects[i].origin.x ;
if (((x >= - EPSINON) && (x <= EPSINON)) && ((y >= - EPSINON) && (y <= EPSINON))){
//这个i返回的只是一个索引,如果帧数是从1开始计算就要+1
currentAnimIndex = i+1;
}
}
CCLog("current frame is %d",currentAnimIndex);