cocos2d-x动画

原创
2012/06/19 10:14
阅读数 2.7K
//使用 plist 的动画
	CCSpriteFrameCache *sfcache = CCSpriteFrameCache::sharedSpriteFrameCache();
	sfcache->addSpriteFramesWithFile("yy/yy.plist");

	CCMutableArray<CCSpriteFrame*> *anim_frames_go = new CCMutableArray<CCSpriteFrame*>();
	char tmp[50];
	for (int j = 0; j < 5; j++)
	{
		sprintf(tmp, "yy_%d.png", j);
		CCSpriteFrame *frame = sfcache->spriteFrameByName(tmp);
		anim_frames_go->addObject(frame);
	}
	CCAnimation *animation_go = CCAnimation::animationWithFrames(anim_frames_go, 0.2f);
	anim_frames_go->release();
	CCAnimate animate_go = CCAnimate::actionWithAnimation(animation_go, true);
	sprite->runAction(CCRepeatForever::actionWithAction(animate_go));


	//不使用 plist 的
	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("yy/yy.png");
	CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*0, 112, 64));
	CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*1, 64*0, 112, 64));
	CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*1, 112, 64));
	CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*1, 64*1, 112, 64));
	CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*2, 112, 64));

	CCMutableArray<CCSpriteFrame*> *anim_frames_go = new CCMutableArray<CCSpriteFrame*>(5);
	anim_frames_go->addObject(frame0);
	anim_frames_go->addObject(frame1);
	anim_frames_go->addObject(frame2);
	anim_frames_go->addObject(frame3);
	anim_frames_go->addObject(frame4);

	CCAnimation *animation_go = CCAnimation::animationWithFrames(anim_frames_go, 0.2f);
	anim_frames_go->release();

	CCAnimate *animate_go = CCAnimate::actionWithAnimation(animation_go, true);
	msprite->setDisplayFrame(frame0);
	sprite->runAction(CCRepeatForever::actionWithAction(animate_go));

看上去代码量着不多,但是使用 plist 的话方便管理与使用。


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
1
分享
返回顶部
顶部