触发器扩展

内容地址: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

<?xmlversion="1.0" encoding="utf-8"?>

<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"

<?xmlversion="1.0" encoding="utf-8"?>

<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
这个,因为这里的内容类型较为全面

<?xmlversion="1.0" standalone="yes" ?>

<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

表示没看到编辑器截图的说,莫非还没写完的 :3:

居然敢不回我贴,强势来占楼:2:

http://www.cocoachina.com/bbs/read.php?tid=194648
这个是原文
关于触发器拖动问题,还有动作顺序和拖动问题求结局 :6:
还有生成的时候,只会生成C++代码,那我的lua需要自己根据生成的代码来写? :8:

估计是图挂了~~内容就这么多,后面可能会做优化,我会持续更新。

http://www.cocoachina.com/bbs/read.php?tid=194648
帖子中的@chengstory是我们的开发人员。

cocos2d-x rc 版本里是 cocostudio::BaseTriggerAction,而cocostuduio触发器生成代码里却是 cocos2d::extension::BaseTriggerCondition??要怎么修改模板呢?

@crystal_ooo 我去看看這個問題

模板在C:\Users***\Documents\CocoStudio\Samples\Trigger下,我已经改了。改完后重新生成一下就OK了。

先顶楼主
我用的版本是1.4.01,修改了…Samples\Scene\FightScene\GameFightScene\CocoStudio\ccsprojs\FightScene\Code\cpp\3.x\cons.cpp的代码,在cocostudio 模拟器里面却看不到效果,相同代码在vs里面编译之后有效果的。想知道怎么让模拟器也能实现…

需要设置模拟器的路径。设置入口在菜单栏中的“设置”选项中。注意修改工程要以我们提供的为模板,因为https://github.com/chukong/CocoStudioConnector包含了差数的传递。

是要下载模拟器自己修改编译的吧,下好之后有各种错误没编译成功,我看工程目录都是比较早的,CocoStudio等库都是在libExtension里面的,是需要什么配置?或者是有新一点的CocoStudioConnector下载没

这个是2.x的版本,如果需要3.0的可以看这里去修改:http://www.cocoachina.com/bbs/read.php?tid=196660

我用的是2.x版本的,请问要怎么修改模拟器代码,然后设置到场景编辑器上,才能正常播放扩展的触发器效果,有没有介绍CocoStudioConnector用法的文档,谢谢