Cocos2d-x学习笔记(七)—— 动作处理

Cocos2d-x学习笔记(七)—— 动作处理,第1张

概述动作类型 瞬时动作: 瞬间执行的动画,我们无法看到它变化的过程。 CCSize s = Director::getInstance()->getWinSize(); CCLabelTTF *label = CCLabelTTF::create("windowslake", "A Damn Mess", 23); label->setPosition(Vec2(0, s.height / 2)

动作类型

瞬时动作:

瞬间执行的动画,我们无法看到它变化的过程。

CCSize s = Director::getInstance()->getWinSize();	cclabelTTF *label = cclabelTTF::create("windowslake","A damn Mess",23);	label->setposition(Vec2(0,s.height / 2));	this->addChild(label,1);	// 根据提供的点执行设置位置动作	CCPlace *place = CCPlace::create(Vec2(s.wIDth / 2,s.height / 2));	label->runAction(place);	// 隐藏动作(使我们的控件不可见)	CCHIDe *hIDe = CCHIDe::create();	label->runAction(hIDe);		// 显示动作	CCShow *show = CCShow::create();	label->runAction(show);	// 当控件可见时,使其隐藏,当其隐藏时,使其可见	CCToggleVisibility *tv = CCToggleVisibility::create();	label->runAction(tv);	CCToggleVisibility *tv2 = CCToggleVisibility::create();	label->runAction(tv2);	// 使控件延中心点做x轴翻转	//CCFlipX *flipx = CCFlipX::create(true);	//label->runAction(flipx);	//// 使控件延中心点做y轴翻转	//CCFlipY *flipy = CCFlipY::create(true);	//label->runAction(flipy);

延时动画:

逐渐执行的动画,有渐变性,我们可以看到它的改变,这里只是列举几种。

// 闪烁	CCBlink *blink = CCBlink::create(3.0f,10);	label->runAction(blink);	// 颜色转变	CCTintTo *tintto = CCTintTo::create(3.0f,255,0);	label->runAction(tintto);	// 从隐藏到显示	CCFadeIn *fadein = CCFadeIn::create(3.f);	label->runAction(fadein);

组合动作:

将多种动作组合起来播放,可同步也可异步。

// 用序列执行异步动画,即一个一个执行,参数为NulL时,表示添加动画效果结束	CCFiniteTimeAction *sequence = CCSequence::create(fadeout,tintto,NulL);	label->runAction(sequence);	// 用序列执行同步动画,全部一起执行,参数为NulL时,表示添加动画效果结束	CCFiniteTimeAction *spawn = CCSpawn::create(fadeout,NulL);	label->runAction(spawn);	// 重复执行动作,参数2为执行次数	CCFiniteTimeAction *sequence = CCSequence::create(fadeout,fadein,NulL);	CCRepeat *repeat = CCRepeat::create(sequence,5);	label->runAction(repeat);	// 无限重复执行动作	CCRepeatForever *repeatforever = CCRepeatForever::create((CCActionInterval*)sequence);	label->runAction(repeatforever);

还有一种特殊的组合动作,用图片来播放动作,简称动画帧。
// 帧动画,通过图片形式,一帧一帧的播放动画(这里我们有14张图片)	CCSprite *grossini = CCSprite::create("grossini_dance_01.png");	grossini->setposition(s.wIDth / 2,s.height / 2);	this->addChild(grossini);	// 帧数(图片的张数)	int framecount = 15;	// 创建动画帧	CCAnimation *animation = CCAnimation::create();	for (int i = 1; i < framecount; i++)	{		// 字符数组,存储图片名		char str[50] = { 0 };		// 将图片名打印到str		sprintf(str,"grossini_dance_%02d.png",i);		// 创建图片动画帧		animation->addSpriteFrameWithfile(str);	}	// 每个动画帧的间隔时间	animation->setDelayPerUnit(1.0f);	// 通过setRestoreOriginalFrame来设置是否在动画播放结束后恢复到第一帧	animation->setRestoreOriginalFrame(true);	// 创建动画	CCAnimate *animate = CCAnimate::create(animation);	// 执行动画	grossini->runAction(CCRepeatForever::create(animate));

<pre name="code" >/*	* AnimationCache可以加载xml/pList文件,pList文件里保存了组成动画的相关信息,	* 通过该类获取到pList文件里的动画。	*/	CCSprite *grossini = CCSprite::create("grossini_dance_01.png");	grossini->setposition(s.wIDth / 2,s.height / 2);	this->addChild(grossini);	// 获取动画缓存实例对象	auto cache = AnimationCache::getInstance();	// 添加动画文件到缓存,pList文件	cache->addAnimationsWithfile("animations-2.pList");	// 从缓存中获取动画对象	auto animation = cache->getAnimation("dance_1");	auto action = Animate::create(animation);	grossini->runAction(Sequence::create(action,action->reverse(),NulL));
 
动作继承表:

总结

以上是内存溢出为你收集整理的Cocos2d-x学习笔记(七)—— 动作处理全部内容,希望文章能够帮你解决Cocos2d-x学习笔记(七)—— 动作处理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1067956.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存