cocos2d-x学习笔记(7)--动作(Actions)
cocos2d-x学习笔记(7)--动作(Actions)
动作在cocos2d-x中可以说是精华部分(不是说其它的不好,而是这一部分内容特别丰富!!),作为重中之重,我们会分几个阶段来学习,先从简单的动作开始,然后通过组合简单的动作形成复杂动作。
一下就是我们将会用到的主要函数,函数有些多,但不一定全部要学,可以根据自己的需要来由选择性的学习。
1.Manual Transformation
2.MoveTo / MoveBy
3.RotateTo / RotateBy
4.JumpTo / JumpBy
5.Blink
6.FadeIn / FadeOut
7.Tint
8.Animation
9.Sequence: Move + Rotate
10.Sequence of InstantActions
step1:创建一个cocos2d-win32 application,并命名为action;
以下是我将用到的图片素材:
step2:
首先,定义一些如下常量和变量:
[cpp]
static int index = 1;//浏览到第几个动作
const int MAX_INDEX = 10;//总共要展示的动作种类数目
enum
{
MANUAL_ACTION = 1,
MOVE_ACTION,
ROTATE_ACTION,
JUMP_ACTION,
BLINK_ACTION,
FADE_ACTION,
TINT_ACTION,
ANIMATE_ACTION,
SEQUENCE_ACTION_1,
SEQUENCE_ACTION_2
};
创建一个基本的动作类(仅仅包含三个操作按钮),然后让其他的动作类继承此基础类,可以减少重复写按钮操作函数。
在HelloWorldScene.h中添加如下动作类:
[cpp]
class BasicActions:public CCLayer
{
protected:
CCSprite* m_player;
public:
virtual void onEnter();
virtual void onExit();
virtual std::string title();
void nextMenuCallback(CCObject* pSender);
void backMenuCallback(CCObject* pSender);
void restartMenuCallback(CCObject* psender);
};
//manual action
//基本的动作
class ActionManual:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//move action
//对象移动操作
class ActionMove:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//rotate action
//旋转动作
class ActionRotate:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//jump action
//跳跃动作
class ActionJump:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//blink action
//闪烁动作
class ActionBlink:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//fade action
//淡化动作
class ActionFade:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//tint action
//着色动作
class ActionTint:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//animate action
//动画(由帧组成)
class ActionAnimate:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//sequence action1
//连贯动作
class ActionSequence:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
};
//sequence action2
//连贯动作,带有事件响应
class ActionSequence2:public BasicActions
{
public:
virtual void onEnter();
virtual std::string title();
public:
void callback1();
void callback2();
};
由于在点击“back”、“next”两个按钮时会进行背景的切换,以更换动作:为了方便操作,我在HelloWorldScene.cpp中添加了一个函数用于操作对应动作类的创建:
[cpp]
CCLayer* runThisTest(int index)
{
CCLayer* pLayer = NULL;
switch (index)
{
case MANUAL_ACTION:
pLayer = new ActionManual(); break;
case MOVE_ACTION:
pLayer = new ActionMove();break;
case ROTATE_ACTION:
pLayer = new ActionRotate();break;
case JUMP_ACTION:
pLayer = new ActionJump();break;
case BLINK_ACTION:
pLayer = new ActionBlink();break;
case FADE_ACTION:
pLayer = new ActionFade();break;
case TINT_ACTION:
pLayer = new ActionTint();break;
case ANIMATE_ACTION:
pLayer = new ActionAnimate();break;
case SEQUENCE_ACTION_1:
pLayer = new ActionSequence();break;
case SEQUENCE_ACTION_2:
pLayer = new ActionSequence2();break;
}
return pLayer;
}
在runThisTest函数中index用来标记当前处于那个背景,以便创建响应的动作,这里我把BasicAction类中的成员函数定义罗列出来,方便大家理解runThisTest函数的用法:
[cpp]
void BasicActions::onEnter()
{
CCLayer::onEnter();
CCSize size = CCDirector::sharedDirector()->getWinSize();
//添加基本角色
m_player = CCSprite::spriteWithFile("player.png");
m_player->retain();//此处涉及到指针等内存管理机制,目前暂不展开做详细讨论
补充:移动开发 , 其他 ,