Learning Cocos2d-x for WP8(8)——动作Action
原文:Learning Cocos2d-x for WP8(8)——动作Action
游戏很大程度上是由动作画面支撑起来的。
动作分为两大类:瞬间动作和延时动作。
瞬间动作基本等同于设置节点的属性,延时动作会执行一段时间,不需要清除这两种动作,一旦动作完成,就会从节点上清除并释放所占内存。
封装CCSpriteBatchNode的使用方法
CCSprite* SpriteActionLayer::BatchAnimatitonSprite(CCLayer* pLayer,CCSprite *pSprite, CCPoint* spriteStartPoint, const char *strFramesFileName,const char *strFramesFileNameImg,const char *strFirstFrameName,const char *strEachFrameName,int frameCount,float frameDelay)
{
//CCSpriteBatchNode
//创建批处理节点,读取plist文件
CCSpriteFrameCache* cache=CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile(strFramesFileName,strFramesFileNameImg); //起始精灵
pSprite=CCSprite::spriteWithSpriteFrameName(strFirstFrameName);//纹理plist中包含strfirstFrameName
pSprite->setPosition(ccp(spriteStartPoint->x,spriteStartPoint->y)); CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::batchNodeWithFile(strFramesFileNameImg);//与CCSpriteFrameCache同一纹理
spritebatch->addChild(pSprite);
pLayer->addChild(spritebatch,); //创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(frameCount);
char str1[]={};
for(int i=;i<frameCount;i++)
{
sprintf(str1,strEachFrameName,i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
} CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,frameDelay);
pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false))); animFrames1->release();
return pSprite;
}
重复动作
可以使一个动作或一系列的动作不停地重复无限循环,除非“外力”使其停止。
实例
//重复动作
CCSprite *role0=CCSprite::spriteWithFile("Sprite/Role.png");
role0->setPosition(ccp(,s.height-));
addChild(role0,);
CCRotateBy* role0RotateBy=CCRotateBy::actionWithDuration(2.0f,);
CCRepeatForever* role0Repeat=CCRepeatForever::actionWithAction(role0RotateBy);
role0->runAction(role0Repeat);
Sprite循环不停地旋转

流畅动画
CCEaseAction类能在一段时间内动作的流畅执行,使得更强大。而非简单地执行简单动画效果。
在实际运动过程中,匀速运动在启动和结束时往往会有一定的加速和减速的效果,这样更加的真实。
cocoos2d-x引擎提供了相关的API,免除了我们编写相关的算法实现的烦恼,实现起来相当的方便。
实现该方法的是CCActionEase中CCEaseRateAction系列,大体分成三类:
In:开始时候的加速度
Out:结束时候的加速度
InOut:开始结束时候的加速度
(以下图片来源其他博文,具体地址忘记了,以后再加上)
1.指数缓冲

EaseExponentialIn
EaseExponentialOut
EaseExponentialInOut
2.赛因缓冲

EaseSineIn
EaseSineOut
EaseSineInOut
3.弹性缓冲

EaseElasticIn
EaseElasticOut
EaseElasticInOut
4.跳跃缓冲

EaseBounceIn
EaseBounceOut
EaseBounceInOut
5.回震缓冲

EaseBackIn
EaseBackOut
EaseBackInOut
实例
//流畅动画
CCSprite* role1=NULL;
role1=SpriteActionLayer::BatchAnimatitonSprite(this,role1,new CCPoint(,s.height/),"Sprite/Plist/RoleRun.plist","Sprite/Plist/RoleRun.png","RoleRun0.png","RoleRun%d.png",,0.2f);
CCMoveTo* role1Move=CCMoveTo::actionWithDuration(,CCPointMake(s.width-,s.height/));//CCMoveTo绝对位置移动
CCEaseInOut* role1MoveEase=CCEaseInOut::actionWithAction(role1Move,);//rate=4,决定流畅动画的明显程度,只有当它大于1时才能看到效果
role1->runAction(role1MoveEase);
可见,Sprite开始缓慢移动——加速——缓慢停止。


动作序列
通常情况下,Action动作执行添加多个动作时,它们会在同一时间运行,如上面的动作。但有时我们需要让动作一个接着一个运行。CCSequence将实现该功能。
实例
//动作序列
CCSprite* role2=NULL;
role2=SpriteActionLayer::BatchAnimatitonSprite(this,role2,new CCPoint(,s.height/),"Sprite/Plist/Role2Run.plist","Sprite/Plist/Role2Run.png","Role2Run0.png","Role2Run%d.png",,0.3f);
CCMoveBy* role2MoveBy=CCMoveBy::actionWithDuration(,CCPointMake(,));//CCMoveBy相对位置移动
CCJumpBy* role2JumpBy=CCJumpBy::actionWithDuration(,CCPointMake(-,),,);//CCJumpBy相当位置jump
role2->runAction(CCSequence::actions(role2MoveBy,role2JumpBy,NULL));
Sprite跑动到指定位置后,下个动作就是弹跳回起始位置


瞬间动作
有时在一串动作中,需要改变Sprite的属性,然后在继续执行下个动作。瞬间动作将实现该功能,通常情况下依赖与CCCallFunc动作。
实例
//瞬时动作
CCSprite* role3=CCSprite::spriteWithFile("Sprite/Role.png");
role3->setPosition(ccp(s.width-,s.height-));
addChild(role3,);
CCScaleTo * role3ScaleTo=CCScaleTo::actionWithDuration(,2.0f);
CCCallFuncN* role3FunN=CCCallFuncN::actionWithTarget(this,callfuncN_selector(SpriteActionLayer::repeatForever));
role3->runAction(CCSequence::actions(role3ScaleTo,role3FunN,NULL));
repeatForever方法
void SpriteActionLayer::repeatForever(CCNode* pSender)
{
CCRepeatForever* repeat=CCRepeatForever::actionWithAction(CCSkewBy::actionWithDuration(, 37.2f, -37.2f));//歪斜
pSender->runAction(repeat);
}
执行完ScaleTo后执行SkewBy

完整源码
#ifndef _SPRITE_ACTION_TEST_
#define _SPRITE_ACTION_TEST_ #include "cocos2d.h"
using namespace cocos2d; class SpriteActionScene:public CCScene
{
public:
SpriteActionScene();
~SpriteActionScene(); virtual void onEnter(); }; class SpriteActionLayer:public CCLayer
{
public:
SpriteActionLayer();
~SpriteActionLayer(); public:
void repeatForever(CCNode* pTarget);
CCSprite* BatchAnimatitonSprite(CCLayer* pLayer,CCSprite *pSprite,CCPoint* spriteStartPoint,const char *strFramesFileName,const char *strFramesFileNameImg,const char *strFirstFrameName,const char *strEachFrameName,int frameCount,float frameDelay);
}; #endif
#include "pch.h"
#include "Classes\SpriteActionTest.h" //----------------------------------------
//
//SpriteActionLayer
//
//----------------------------------------
SpriteActionLayer::SpriteActionLayer()
{
CCSize s=CCDirector::sharedDirector()->getWinSize(); //重复动作
CCSprite *role0=CCSprite::spriteWithFile("Sprite/Role.png");
role0->setPosition(ccp(,s.height-));
addChild(role0,);
CCRotateBy* role0RotateBy=CCRotateBy::actionWithDuration(2.0f,);
CCRepeatForever* role0Repeat=CCRepeatForever::actionWithAction(role0RotateBy);
role0->runAction(role0Repeat); //流畅动画
CCSprite* role1=NULL;
role1=SpriteActionLayer::BatchAnimatitonSprite(this,role1,new CCPoint(,s.height/),"Sprite/Plist/RoleRun.plist","Sprite/Plist/RoleRun.png","RoleRun0.png","RoleRun%d.png",,0.2f);
CCMoveTo* role1Move=CCMoveTo::actionWithDuration(,CCPointMake(s.width-,s.height/));//CCMoveTo绝对位置移动
CCEaseInOut* role1MoveEase=CCEaseInOut::actionWithAction(role1Move,);//rate=4,决定流畅动画的明显程度,只有当它大于1时才能看到效果
role1->runAction(role1MoveEase); //动作序列
CCSprite* role2=NULL;
role2=SpriteActionLayer::BatchAnimatitonSprite(this,role2,new CCPoint(,s.height/),"Sprite/Plist/Role2Run.plist","Sprite/Plist/Role2Run.png","Role2Run0.png","Role2Run%d.png",,0.3f);
CCMoveBy* role2MoveBy=CCMoveBy::actionWithDuration(,CCPointMake(,));//CCMoveBy相对位置移动
CCJumpBy* role2JumpBy=CCJumpBy::actionWithDuration(,CCPointMake(-,),,);//CCJumpBy相当位置jump
role2->runAction(CCSequence::actions(role2MoveBy,role2JumpBy,NULL)); //瞬时动作
CCSprite* role3=CCSprite::spriteWithFile("Sprite/Role.png");
role3->setPosition(ccp(s.width-,s.height-));
addChild(role3,);
CCScaleTo * role3ScaleTo=CCScaleTo::actionWithDuration(,2.0f);
CCCallFuncN* role3FunN=CCCallFuncN::actionWithTarget(this,callfuncN_selector(SpriteActionLayer::repeatForever));
role3->runAction(CCSequence::actions(role3ScaleTo,role3FunN,NULL));
} void SpriteActionLayer::repeatForever(CCNode* pSender)
{
CCRepeatForever* repeat=CCRepeatForever::actionWithAction(CCSkewBy::actionWithDuration(, 37.2f, -37.2f));//歪斜
pSender->runAction(repeat);
} SpriteActionLayer::~SpriteActionLayer()
{} CCSprite* SpriteActionLayer::BatchAnimatitonSprite(CCLayer* pLayer,CCSprite *pSprite, CCPoint* spriteStartPoint, const char *strFramesFileName,const char *strFramesFileNameImg,const char *strFirstFrameName,const char *strEachFrameName,int frameCount,float frameDelay)
{
//CCSpriteBatchNode
//创建批处理节点,读取plist文件
CCSpriteFrameCache* cache=CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile(strFramesFileName,strFramesFileNameImg); //起始精灵
pSprite=CCSprite::spriteWithSpriteFrameName(strFirstFrameName);//纹理plist中包含strfirstFrameName
pSprite->setPosition(ccp(spriteStartPoint->x,spriteStartPoint->y)); CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::batchNodeWithFile(strFramesFileNameImg);//与CCSpriteFrameCache同一纹理
spritebatch->addChild(pSprite);
pLayer->addChild(spritebatch,); //创建逐帧数组
CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(frameCount);
char str1[]={};
for(int i=;i<frameCount;i++)
{
sprintf(str1,strEachFrameName,i);
CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 );
animFrames1->addObject(pFrame);
} CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,frameDelay);
pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false))); animFrames1->release();
return pSprite;
} //----------------------------------------
//
//SpriteActionScene
//
//---------------------------------------- SpriteActionScene::SpriteActionScene()
{} SpriteActionScene::~SpriteActionScene()
{} void SpriteActionScene::onEnter()
{
CCScene::onEnter();
CCLayer* spriteActionLayer=new SpriteActionLayer();
addChild(spriteActionLayer);
spriteActionLayer->release();
}

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
Learning Cocos2d-x for WP8(8)——动作Action的更多相关文章
- Java基础之处理事件——使用动作Action(Sketcher 6 using Action objects)
控制台程序. 动作Action是任何实现了javax.swing.Action接口的类的对象.这个接口声明了操作Action对象的方法,例如,存储与动作相关的属性.启用和禁用动作.Action接口扩展 ...
- 08 Zabbix4.0系统配置事件通知 - 动作Action
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 08 Zabbix4.0系统配置事件通知 - 动作Action 请点击查看Zabbix3.0.8版 ...
- [Cocos2d-x For WP8]ActionManager动作管理
在Cocos2d-x里面可以通过CCActionManger类来管理动作的暂停和恢复,CCActionMessage是管理所有Action的单例,一般情况下并不直接使用这个单例,而是使用CCNode的 ...
- 动作Action
/** * DelayTime延迟 * @param d Duration 延迟时间 */ auto delayTime = DelayTime::create(); sprite->runAc ...
- 创建一个动作-Action类:
让我们创建一个Java文件HelloWorldAction.java的Java资源> SRC下一个的包名com.yiibai.struts2与下面的内容. package com.yiibai. ...
- libgdx学习记录6——动作Action
libgdx中的Action类能够有效的帮助我们实现位移.旋转.缩放.淡入淡出等效果,对游戏的设计很有用. Action是一个抽象类,本身不可以实例化.一般使用的它的继承类,常用的有 MoveToAc ...
- Asp.Net MVC学习总结(二)——控制器与动作(Controller And Action)
一.理解控制器 1.1.什么是控制器 控制器是包含必要的处理请求的.NET类,控制器的角色封装了应用程序逻辑,控制器主要是负责处理请求,实行对模型的操作,选择视图呈现给用户. 简单理解:实现了ICon ...
- Struts2 In Action笔记_页面到动作的数据流入和流出
因为回答百度知道的一个问题,仔细查看了<Struts2 In Action>,深入细致的看了 “数据转移OGNL 和 构建视图-标签”,很多东西才恍然大悟. 一直觉得国外写的书很浮,不具有 ...
- 行为识别(action recognition)相关资料
转自:http://blog.csdn.net/kezunhai/article/details/50176209 ================华丽分割线=================这部分来 ...
随机推荐
- MySql截取DateTime字段的日期值
用 DATE_FORMAT 来格式化日期字段 SELECT DATE_FORMAT(crt_time,'%Y-%m-%d') FROM ad_n_advertise_t
- Swift - 字符串(String)用法详解
下面对String常用的属性和方法做个总结 1,判断是否为空:isEmpty 1 2 3 var str:String if str.isEmpty{ } 2,获取字符数量:countElements ...
- UIGestureRecognizer在多层视图中的触发问题
在一个superview中,添加了一个subview.tap一下superview,将subview隐藏起来. 在视图superview添加一个UITapGestureRecognizer对象,在UI ...
- C++学习之路—引用(一)—基础知识
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 对一个数据可以建立一个“引用”,它的作用是为一个 ...
- 让进程在后台可靠运行的几种方法 nohup,setsid,&,disown,CTRL-z ,screen
让进程在后台可靠运行的几种方法 几年前在developerWorks上面看到的文章,感觉非常实用,又简单整理了一下,转到这里,希望给看到的人带来一些帮助.文中提到的nohup和subshell方式一直 ...
- Android broadcast
发送广播而且接受.发送两个广播 Intent intent = new Intent(); intent.setAction("com.wxq.CUSTOM_INTENT"); s ...
- MFC的消息反射机制
1.消息反射解释: 父窗口将子窗口发给它的通知消息,首先反射回子窗口进行处理(即给子窗口一个机会,让子窗口处理此消息),这样通知消息就有机会能被子窗口自身进行处理. 2.MFC中引入消息反射的原因: ...
- 让盘古分词支持最新的Lucene.Net 3.0.3
原文:让盘古分词支持最新的Lucene.Net 3.0.3 好多年没升级过的Lucene.Net最近居然升级了,到了3.0.3后接口发生了很大变化,原来好多分词库都不能用了,所以上次我把MMSeg给修 ...
- redis 获取key 过期时间
<pre name="code" class="html">127.0.0.1:6379> keys *b4f107c6-e96c-4a1e- ...
- 如何设置lmt的空间警告阀值
Example—设置Locally Managed Tablespace的空间警告阀值 The following example sets the free-space-remaining thre ...