1
缓冲动作
man->setScale(0.8f);
woman->setScale(0.8f);
man->setPosition(ccp(100, 80));
woman->setPosition(ccp(100,240));
CCMoveTo * manTo = CCMoveTo::create(2, ccp(400,80));
CCMoveTo * womanTo = CCMoveTo::create(2, ccp(400, 240));
CCEaseExponentialIn *in = CCEaseExponentialIn::create(manTo);
CCEaseExponentialOut *out = CCEaseExponentialOut::create(manTo);
CCEaseExponentialInOut * inOut =
CCEaseExponentialInOut::create(manTo);
man->runAction(inOut);
woman->runAction(womanTo);
CCEaseElasticIn *in = CCEaseElasticIn::create(manTo);
CCEaseElasticOut *out = CCEaseElasticOut::create(manTo);
CCEaseElasticInOut * inOut = CCEaseElasticInOut::create(manTo);
#include
"T13Action.h"
#include
"AppMacros.h"
CCScene *T13Action::scene()
{
CCScene *
scene =
CCScene::create();
T13Action *
layer =
T13Action::create();
scene->addChild(layer);
return
scene;
}
bool
T13Action::init()
{
TBack::init();
CCSprite *
man =
CCSprite::create("man.png");
CCSprite *
woman =
CCSprite::create("woman.png");
man->setPosition(ccp(100,
160));
CCMoveBy *by
= CCMoveBy::create(2,ccp(300,0));
CCMoveBy
*by2
= CCMoveBy::create(2,ccp(300,0));
CCEaseExponentialIn * in =
CCEaseExponentialIn::create(by2);
CCEaseExponentialInOut
* out =
CCEaseExponentialInOut::create(by2);
CCEaseExponentialInOut *
inOut =
CCEaseExponentialInOut::create(by2);
//CCEaseElasticIn * in = CCEaseElasticIn::create(by2);
//CCEaseElasticOut * out = CCEaseElasticOut::create(by2);
//CCEaseElasticInOut * inOut = CCEaseElasticInOut::create(by2);
//CCEaseBounceIn
*in = CCEaseBounceIn::create(by2);
//CCEaseBounceOut * out = CCEaseBounceOut::create(by2);
//CCEaseBounceInOut * inOut = CCEaseBounceInOut::create(by2);
//看精灵的运行速度
//man->runAction(in);
//man->runAction(out);
man->runAction(inOut);
addChild(man);
return
true;
}
//原生绘图,每一帧都会绘图
void
T13Action::draw()
{
CCPointArray *array =
CCPointArray::create(6);
array->addControlPoint(ccp(100,
160));
array->addControlPoint(ccp(200,
160));
array->addControlPoint(ccp(200,
250));
array->addControlPoint(ccp(300,
250));
array->addControlPoint(ccp(300,
160));
array->addControlPoint(ccp(450,
160));
ccDrawCardinalSpline(array, 1, 100);
}
运行结果:
2
并行动作和序列动作
CCSequence
中持续时间为所有动作累计的总合,CCSequence
中不可以
有CCRepeatForever
动作。
CCSpwan中持续时间为并行动作持续时间最长的决定。
3
假动作
CCCallFunc系列动作包括CCallFunc()、CCCallFuncN()、CCCallFuncND,以及CCCallFuncO四个动作,CCCallFunc系列动作的后缀”N”表示Node参数,指的是执行动作的对象,”D”表示Data参数,指的是用户自定义的数据,”O”表示对象,指的是一个用户自定义的CCObject参数。在不同的情况下,我们可以根据不同的需求来选择不同的CCCallFunc动作。
typedef void (CCObject::*SEL_CallFunc)();
//无参类型
typedef void (CCObject::*SEL_CallFuncN)(CCNode*);
//传递CCNode *
类型变量
typedef void (CCObject::*SEL_CallFuncND)(CCNode*, void*);
//传递CCNode *
类型变量,和无类型变量
typedef void (CCObject::*SEL_CallFuncO)(CCObject*);
//传递CCObject *类型变量
#define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR)
#define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(&_SELECTOR)
#define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(&_SELECTOR)
#define callfuncO_selector(_SELECTOR) (SEL_CallFuncO)(&_SELECTOR)
CCCallFunc * func = CCCallFunc::create(this,
callfunc_selector(T13Action::funcCallBack));
CCCallFuncN * func = CCCallFuncN::create(this,
callfuncN_selector(T13Action::funcNCallBack));
CCCallFuncND * func = CCCallFuncND::create(this,
callfuncND_selector(T13Action::funcNDCallBack), (void *)"30");
CCArray * array = CCArray::create();
array->addObject(woman);
CCCallFuncO * func = CCCallFuncO::create(this,
callfuncO_selector(T13Action::funcOCallBack),(CCObject*)array);
CCSequence * seq = CCSequence::create(to, func, NULL);
man->runAction(seq);
addChild(man);
案例说明:
无参的假动作
void
funcCallBack();
T13Action.h
#ifndef
_T13Action_H__
#define
_T13Action_H__
#include
"cocos2d.h"
#include
"TBack.h"
USING_NS_CC;
class
T13Action :public
TBack
{
public:
static
CCScene *
scene();
CREATE_FUNC(T13Action);
bool
init();
void
draw();
//无参的假动作
void
funcCallBack();
//带调用者的假动作
void
funcNCallBack(CCNode
* node);
//带有调用者和数据的假动作
void
funcNDCallBack(CCNode
* node,
void *
data);
//带有对象的假动作
void
funcOCallBack(CCObject
* obj);
};
#endif
T13Action.cpp
#include
"T13Action.h"
#include
"AppMacros.h"
CCScene *T13Action::scene()
{
CCScene *
scene =
CCScene::create();
T13Action *
layer =
T13Action::create();
scene->addChild(layer);
return
scene;
}
bool
T13Action::init()
{
TBack::init();
CCSprite *
man =
CCSprite::create("man.png");
CCSprite *
woman =
CCSprite::create("woman.png");
man->setPosition(ccp(100,
160));
CCMoveBy *by2
= CCMoveBy::create(2,
ccp(300, 0));
//假动作就是当一个动作执行完了之后又执行的一个动作
CCCallFunc *
func =
CCCallFunc::create(this,callfunc_selector(T13Action::funcCallBack));
CCSequence *
seq =
CCSequence::create(by2,
func,
NULL);
man->runAction(seq);
addChild(man);
return
true;
}
//原生绘图,每一帧都会绘图
void
T13Action::draw()
{
CCPointArray *array =
CCPointArray::create(6);
array->addControlPoint(ccp(100,
160));
array->addControlPoint(ccp(200,
160));
array->addControlPoint(ccp(200,
250));
array->addControlPoint(ccp(300,
250));
array->addControlPoint(ccp(300,
160));
array->addControlPoint(ccp(450,
160));
ccDrawCardinalSpline(array, 1, 100);
}
//无参的假动作
void
T13Action::funcCallBack()
{
CCLog("action
is over");
}
运行结果(运行速度是变速的):
输出结果:
4
假动作
#ifndef
_T13Action_H__
#define
_T13Action_H__
#include
"cocos2d.h"
#include
"TBack.h"
USING_NS_CC;
class
T13Action :public
TBack
{
public:
static
CCScene *
scene();
CREATE_FUNC(T13Action);
bool
init();
//无参的假动作
void
funcCallBack();
//带调用者的假动作
void
funcNCallBack(CCNode
* node);
//带有调用者和数据的假动作
void
funcNDCallBack(CCNode
* node,
void *
data);
//带有对象的假动作
void
funcOCallBack(CCObject
* obj);
};
#endif
#include
"T13Action.h"
#include
"AppMacros.h"
CCScene *T13Action::scene()
{
CCScene *
scene =
CCScene::create();
T13Action *
layer =
T13Action::create();
scene->addChild(layer);
return
scene;
}
bool
T13Action::init()
{
TBack::init();
CCSprite *
man =
CCSprite::create("man.png");
CCSprite *
woman =
CCSprite::create("woman.png");
man->setPosition(ccp(50,
160));
woman->setPosition(ccp(50,260));
//创建一个CCArray,在使用前调用retain(),要注意的是在最后退出之
//后要在onExit()中将array进行release()
CCArray * array =
CCArray::create();
array->retain();
array->addObject(woman);
CCMoveBy *by2
= CCMoveBy::create(2,
ccp(300, 0));
CCCallFuncN *
funcN =
CCCallFuncN::create(this,
callfuncN_selector(T13Action::funcNCallBack));
//调用
CCCallFuncND *funcND
= CCCallFuncND::create(this,
callfuncND_selector(T13Action::funcNDCallBack),
(void*)"30");
CCCallFuncO
* funcO =
CCCallFuncO::create(this,
callfuncO_selector(T13Action::funcOCallBack),
array);
CCSequence *
seq =
CCSequence::create(by2,
funcO,
NULL);
man->runAction(seq);
addChild(man);
return
true;
}
//其中CCNode表示的是动作的执行者
void
T13Action::funcNCallBack(CCNode
*node)
{
CCSprite *spr
= (CCSprite *)node;
//将精灵从屏幕上
//spr->removeFromParentAndCleanup(true);
CCJumpBy *
by =
CCJumpBy::create(2,
ccp(0,0),100,1);
spr->runAction(by);
}
void
T13Action::funcNDCallBack(CCNode
* node,
void *
data)
{
CCSprite *spr
= (CCSprite *)node;
//下面是取出数据的代码
CCString *
str =
CCString::createWithFormat("%s",
(char*)data);
//给精灵旋转
spr->setRotation(str->floatValue());
}
void
T13Action::funcOCallBack(CCObject
* obj)
{
CCArray * array = (CCArray*)obj;
//从array中取出第0个元素
CCSprite *
spr = (CCSprite*)array->objectAtIndex(0);
addChild(spr);
}