原文: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 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!