Cocos2d-x学习(二):动作Action(延时类动作)
游戏中的大部分精灵都是动态的,所以动作类是游戏引擎中不可或缺的一部分,今天就简单的记录下Cocos2d-x中常用的动作类。
Cocos2d-x提供了很多基本的动作类,主要包括两大类:一类是瞬时动作(CCActionInstant),一类是延时动作(CCActionInterval),今天主要记录一下延时动作类的一般用法。
1.Cocos2d-x提供的基本延时类动作都提供了ActionTo和ActionBy两种:(Action泛指各种动作)
ActionTo是指动作结束的状态,也就是动作的终点。
ActionBy是指动作执行的状态,也就是动作的执行过程。
常用的延时动作类包括:
移动动作:CCMoveBy CCMoveTo
旋转动作:CCRotateBy CCRotateTo
缩放动作:CCScaleBy CCScaleTo
跳跃动作:CCJumpBy CCJumpTo
淡入淡出动作:CCFadeBy CCFadeIn CCFadeOut
贝赛尔曲线动作:CCBezierBy CCBezierTo
......(还有很多种,就不一一列举了,详情查看)
2.一般用法
动作中最简单的就要数移动了,下面就分别以CCMoveBy和CCMoveTo举个例子,代码很简单
[cpp]
void HelloWorld::spriteMove()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCActionInterval *actionTo = CCMoveTo::actionWithDuration(2.0f,
CCPointMake(winSize.width * 3 / 4, winSize.height / 2));
CCActionInterval *actionBy = CCMoveBy::actionWithDuration(2.0f,
CCPointMake(-winSize.width / 2, 0));
CCActionInterval *actionByCopy = (CCActionInterval *) actionBy->copy();
CCActionInterval *actionBack = actionBy->reverse();
oldManSprite->runAction(CCSequence::actions(actionTo, actionByCopy, NULL));
youngSisterSprite->runAction(CCSequence::actions(actionBy, actionBack, NULL));
}
void HelloWorld::spriteMove()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCActionInterval *actionTo = CCMoveTo::actionWithDuration(2.0f,
CCPointMake(winSize.width * 3 / 4, winSize.height / 2));
CCActionInterval *actionBy = CCMoveBy::actionWithDuration(2.0f,
CCPointMake(-winSize.width / 2, 0));
CCActionInterval *actionByCopy = (CCActionInterval *) actionBy->copy();
CCActionInterval *actionBack = actionBy->reverse();
oldManSprite->runAction(CCSequence::actions(actionTo, actionByCopy, NULL));
youngSisterSprite->runAction(CCSequence::actions(actionBy, actionBack, NULL));
}
用法很简单,CCMoveTo中的CCPoint参数就是想要“移动到”的位置点,而CCMoveBy中的CCPoint参数就是分别对应x,y轴对应的偏移量 www.zzzyk.com
(这里是对cocos2d-x中的test示例做了一些修改)
这里需要注意的是actionBy不能重复使用,如果第二次使用的话,会继续从第一次使用到的位置开始继续,不明白的可以尝试一下,现象很明显!
3.动作完成回调
动作一般支持3种回调方式
(1).无发送者无数据回调:CCCallFunc,示例如下
[cpp]
void HelloWorld::actionCallback()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::labelWithString("Action Callback!", "Marker Felt", 32);
label->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(label);
}
void HelloWorld::actionCallback()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *label = CCLabelTTF::labelWithString("Action Callback!", "Marker Felt", 32);
label->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(label);
}
(2).有发送者无数据回调:CCCallFuncN (N是CCNode),示例如下
[cpp]
void HelloWorld::actionCallbackN(CCNode *pSender)
{
CCSprite *sprite = (CCSprite *) pSender;
// 回调变红色
sprite->setColor(ccRED);
}
void HelloWorld::actionCallbackN(CCNode *pSender)
{
CCSprite *sprite = (CCSprite *) pSender;
// 回调变红色
sprite->setColor(ccRED);
}
(3).有发送者有数据回调:CCCallFuncND (N是CCNode,D是data,是void *类型)
[cpp]
void HelloWorld::actionCallbackND(CCNode *pSender, void *data)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *sprite = (CCSprite *) pSender;
// 回调变蓝色
sprite->setColor(ccBLUE);
// 接收回调数据
CCLabelTTF *label = CCLabelTTF::labelWithString((char *) data, "Marker Felt", 32);
label->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(label);
CCLog((char *) data);
}
void HelloWorld::actionCallbackND(CCNode *pSender, void *data)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *sprite = (CCSprite *) pSender;
// 回调变蓝色
sprite->setColor(ccBLUE);
// 接收回调数据
CCLabelTTF *label = CCLabelTTF::labelWithString((char *) data, "Marker Felt", 32);
label->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(label);
CCLog((char *) data);
}
使用回调示例如下:
[cpp]
void HelloWorld::actionListen()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCFiniteTimeAction *action = CCSequence::actions(
CCMoveBy::actionWithDuration(2.0f, CCPointMake(winSize.width / 2, 0)),
CCCallFunc::actionWithTarget(this, callfunc_selector(HelloWorld::actionCallback)), NULL);
CCFiniteTimeAction *actionN = CCSequence::actions(
CCMoveBy::actionWithDuration(2.0f, CCPointMake(-winSize.width / 2, 0)),
CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::actionCallbackN)), NULL);
CCFiniteTimeAction *actionND = CCSequence::actions(
CCMoveBy::actionWithDuration(2.0f, CCPointMake(winSize.width / 2, 0)),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(HelloWorld::actionCallbackND), (void *) "Callback Data"), NULL);
oldManSprite->runAction(actionND);
youngSisterSprite->runAction(actionN);
}
void HelloWorld::actionListen()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
补充:移动开发 , 其他 ,