cocos2d-x 一图多帧 取得当前帧的解决方案


转载请保留此文:

http://blog.csdn.net/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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值