内容地址:C:\Users
$username\Documents\CocoStudio\Samples\Trigger
TriggerXML(用来配置编辑器中的选项)
<img title = '1.png' src='http://cdn.cocimg.com/bbs/attachment/Fid_48/48_183396_5917ca3f6a34802.png' >
Template(程序实现的模板)
Project(已经实现好的触发器程序)

事件
1:配置参数,提供给编辑器使用
编辑C:\Users$
username\Documents\CocoStudio\Samples\Trigger\TriggerXML\Event.xml
<RootType=“Scene”>
<Event ClassName="EnterScene"Name="SceneOnEnter"/>
<Event ClassName="LeaveScene"Name="SceneOnExit"/>
<EventClassName="InitScene" Name="SceneInit"/>
<EventClassName="UpdateScene" Name="SceneUpdate"/>
<EventClassName="TouchBegan" Name="TouchBegan"/>
<EventClassName="TouchMoved" Name="TouchMoved"/>
<EventClassName="TouchEnded" Name="TouchEnded"/>
<Event ClassName="TouchCancelled" Name="TouchCancelled"/>
这个文件中的EventList
就是事件列表,每一行是一个独立的事件,其中ClassName
属性是在程序中使用的名称
,Name
是显示在编辑器中的列表。
配置好后,当通过编辑器生成的时候,将会自动生成以下文件
#ifndef__EVENTDEF__
#define__EVENTDEF__
enum {
TRIGGEREVENT_ENTERSCENE = 0,
TRIGGEREVENT_LEAVESCENE,
TRIGGEREVENT_INITSCENE,
TRIGGEREVENT_UPDATESCENE,
TRIGGEREVENT_TOUCHBEGAN,
TRIGGEREVENT_TOUCHMOVED,
TRIGGEREVENT_TOUCHENDED,
TRIGGEREVENT_TOUCHCANCELLED,
};
#endif
枚举中事件名称统一为”TRIGGEREVENT_”+ ClassName
(来自于Event.xml
的参数).
且统一为大写字符。
(这个保存的可能仅仅是索引,需要确认,若是索引则需要保证事件的顺序)
接下来需要到模拟器工程中的HelloWorldScene
类绑定这个事件,因为整个模拟器只会存在一个场景,即HelloWorldScene
,所以添加的事件都是与这个场景绑定在一起的。
添加方法参考HelloWorldScene
事件创建方法,
boolHelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
sendEvent(TRIGGEREVENT_TOUCHBEGAN);
return true;
}
这个方法是普通的事件响应方法,到调用sendEvent
方法时,就将cocos2d-x
引擎的事件引入到了触发器系统中了。
条件:
条件和事件较类似,但是会复杂些.
首先"C:\Users$
username
\Documents\CocoStudio\Samples\Trigger\TriggerXML\Condition.xml"
<RootType=“Scene”>
<ConditionClassName="TimeElapsed" Name="TimeElapsedTo"/>
<ConditionClassName="ArmatureActionState" Name="ActionState"/>
<ConditionClassName="NodeInRect" Name="IsNodeInRect"/>
<ConditionClassName="NodeVisible" Name="IsNodeVisible"/>
同Event.xml
是保持一致的。
但是条件部分还有一些扩充选项:
进C:\Users$
username
\Documents\CocoStudio\Samples\Trigger\TriggerXML\Condition
文件夹
可以看到,这里有四个文件,每个文件均对应Condition.xml
文件中的一个ClassName
属性值。
我们查看ArmatureActionState.xml
这个,因为这里的内容类型较为全面
<Item Type="IntegerUpDown"Name=“Node Tag” Key = “Tag” Default=“10000” />
<Item Type="TextBox"Name=“ComName” Key =“componentName” Default=“CCArmature” />
<Item Type="TextBox"Name=“AniName” Key =“AnimationName” Default=“run” />
<Item Type="ComboBox"Name=“ActionState” Key = "ActionType"Default=“0”>
<Childes>
<Child Name="START"ID="0"/>
<Child Name="COMPLETE"ID="1"/>
<Child Name="LOOP_COMPLETE" ID="2"/>
</Childes>
编辑器截图
Root
标签下是条件数组,每个Item
是一个状态。
属性中Type
表示类型,Name
是编辑器中显示的属性名,Key
是用于用户输入的属性值,Default
代表Key
的默认值。
这里说一下Type
,Type
一共有IntegerUpDown
、DoubleUpDown
、TextBox
、ComboBox
四种种类型。其中IntegerUpDown
、DoubleUpDown
表示整数型值和双精度小数的素质,可以手动输入数字。TextBox
表示文本型,可以接收一个字符串。ComboBox
表示下拉列表型,提供预设值。
参考ActionState
<ItemType=“ComboBox” Name=“ActionState” Key =“ActionType” Default=“0”>
<Childes>
<Child Name="START"ID="0"/>
<Child Name="COMPLETE"ID="1"/>
<Child Name="LOOP_COMPLETE" ID="2"/>
</Childes>
该Item
包含一个元素表,每个元素均包含一个Name
和
一个ID
。ID
是提供给程序使用的。
以上是文件配置部分属于控制编辑器显示的,下面是条件的实现:
先看C:\Users$
username
\Documents\CocoStudio\Samples\Trigger\Template
文件夹下的cons
类,这两个文件是一个条件制作的模板。
具体实现则是在
“C:\Users
$username
\Documents\CocoStudio\Samples\Trigger\Project\cons.h”
“C:\Users
$username
\Documents\CocoStudio\Samples\Trigger\Project\cons.cpp”
两个文件中。
查看project
文件下的cons
类。
我们拿ArmatureActionState
这个判断条件做讲解
类型是cocos2d::extension::BaseTriggerCondition
头文件截取内容
classArmatureActionState : public cocos2d::extension::BaseTriggerCondition
{
DECLARE_CLASS_INFO
public:
ArmatureActionState(void);//
构造方法
virtual ~ArmatureActionState(void);//
析构方法
virtual bool init();//
初始化方法
virtual bool detect();//
获取判断判断结果接口****
关键点****
virtual void serialize(constrapidjson::Value &val);//
序列化,用于获取编辑器中设置的条件值
virtual void removeAll();//
清理当前条件
void animationEvent(cocos2d::extension::CCArmature*armature, cocos2d::extension::MovementEventType movementType, const char*movementID);
//armature
的动作回调函数,用于更新状态
private:
int _nTag;
std::string _comName;//
()
std::string _aniname;
int _nState;
bool _bSuc;
};
关于属性记录这部分参考serialize
实现方法:
voidArmatureActionState::serialize(const rapidjson::Value &val)
{
int count =DICTOOL->getArrayCount_json(val, "dataitems");
for (int i = 0; i < count; ++i)
{
const rapidjson::Value&subDict = DICTOOL->getSubDictionary_json(val, "dataitems",i);
std::string key =DICTOOL->getStringValue_json(subDict, "key");
if (key == "Tag")
{
_nTag =DICTOOL->getIntValue_json(subDict, "value");
continue;
}
else if (key =="componentName")
{
_comName =DICTOOL->getStringValue_json(subDict, "value");
continue;
}
else if (key =="AnimationName")
{
_aniname =DICTOOL->getStringValue_json(subDict, "value");
continue;
}
else if (key =="ActionType")
{
_nState =DICTOOL->getIntValue_json(subDict, "value");
continue;
}
}
}
其中的判断条件值elseif (key == “componentName”)
就对应的前面提的配置文件中的Key
,如下图:
动作
动作部分在配置文件部分与条件极其类似,所以配置文件部分参考条件即可。下面主要讲程序实现部分的不同之处:
基类是cocos2d::extension::BaseTriggerAction
和“条件判断”的实现多数方法也是类似,但不同的是
detect
()替换为done
()函数,这个函数就是去实现预设的函数。
void TMoveTo::done()
{
do
{
CCNodepNode = SceneReader::sharedSceneReader()->getNodeByTag(_nTag);
CC_BREAK_IF(pNode== NULL);
CCActionInterval actionTo = CCMoveTo::create(_fDuration,_pos);
CC_BREAK_IF(actionTo== NULL);
pNode->runAction(actionTo);
}while (0);
}
/////************************************************************************
bool CONDITION_NAME::check() 模板中是这个函数,实现确是
detect
。




