cocos2d-x CocoStudio中场景触发器(Trigger)的使用和扩展

  场景编辑器中的触发器是一种通过事件触发机制,在特定的事件被触发的时候自动执行自己预先定义的动作或者功能。

编辑器中带有一些默认的事件、条件和动作,当然也可以扩展自定义的事件、条件和动作。

触发器可以创建多个,每个触发器都有可以设置自己的:事件、条件、动作。

事件:

  是指触发监听的条件,在程序代码中,我们要在希望开启触发监听的地方用sendEvent(事件ID)来启动事件监听,这时候才会检测“条件”是否满足。

  默认包含8种事件,分别是SceneOnEnter、SceneOnExit、SceneInit、SceneUpdate;TouchBegan、TouchMoved、TouchEnded、TouchCancelled。

条件:

  用来判断触发当前事件时是否执行预订的动作。只有当条件成立的时候才会继续执行预订的动作。当不包含任何条件的时候,动作会被直接触发。

  当包含多个条件的时候,条件之间的关系为并,只有当所有的条件都是符合的时候才能够执行预订的动作。

  默认提供了TimeElapsedTo、ActionState、IsNodeInRect、IsNodeVisible四种条件,分别对应四个类来检测条件是否满足。

动作:

  当触发当前触发器且条件都成立的情况下,要执行的动作。

  默认提供了PlayMusic、MoveTo、MoveBy、RotateTo、RotateBy、 ScaleTo、ScaleBy、SkewTo、SkewBy、TriggerState、ArmaturePlayAction12种默认动作。

条件和动作都带有属性窗口,在属性里可以设置参数,在代码中可以拿到这些设置过的参数以便处理。

当我们设置好触发器后,点击生成,则会根据当前触发器配置生成代码文件放在Code文件夹内,您可以将这些代码文件直接加入您的项目工程:

最后在确定,保存当前触发器配置。

  下面重点讲解:触发器的扩展

  要添加自己的触发器,那么就需要按照已有触发器的模板来写自己的触发器,模板在CocoStudio项目目录下CocoStudioSamplesTriggerTriggerXML,

这里是所有触发器的定义,xml文件格式:

事件:Event.xml

<?xmlversion="1.0" encoding="utf-8"?> 
<RootType="Scene"> 
  <EventList> 
    <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> 
</Root> 

EventList就是事件列表,每一行是一个独立的事件,其中ClassName属性是在程序中使用的名称,给sendEvent(事件名称)用的Name是显示在编辑器中的列表

扩展添加自己的事件:

<Event ClassName="MyEvent"  Name="MyEvent"/> 

条件:Condition.xml

<?xmlversion="1.0" encoding="utf-8"?> 
<RootType="Scene"> 
  <ConditionList> 
    <Condition ClassName="TimeElapsed" Name="TimeElapsedTo"/> 
         <Condition ClassName="ArmatureActionState" Name="ActionState"/> 
         <Condition ClassName="NodeInRect" Name="IsNodeInRect"/> 
         <Condition ClassName="NodeVisible" Name="IsNodeVisible"/> 
  </ConditionList> 
</Root> 

扩展添加自己的条件;

<Condition ClassName="MyCondition" Name="MyCondition"/>

我们知道条件是有属性的,所以这里边的每一个ClassName都对应一个扩充文件,在Condition文件夹下:

我们如果添加自己的条件,不要忘了扩充。

ArmatureActionState.xml:

<?xmlversion="1.0" standalone="yes" ?> 
<Root> 
  <Item Type="NodeTag" Name="NodeTag" CHName="渲染节点标签" 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> 
  </Item> 
</Root>

  Root标签下是属性状态数组,每个Item是一个属性状态。 属性中Type表示类型,Name是编辑器中显示的属性名,Key是用于用户输入的属性值的索引,通过它在程序中找到用户填入的值,Default代表Key的默认值。

  有一点要说明,如果想要显示中文属性名,需要添加:CHName="目标Id"

<Item Type="NodeTag" Name="NodeTag" CHName="渲染节点标签" Key = "Tag" Default="10000" />

我们可以添加一条Item属性,获取我们想要的用户输入的数据。

  Type通用的IntegerUpDownDoubleUpDownTextBoxComboBox四种种类型。其中IntegerUpDownDoubleUpDown表示整数型值和双精度小数的素质,可以手动输入数字。TextBox表示文本型,可以接收一个字符串。ComboBox表示下拉列表型,提供预设值。 

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

Item是一个下拉列表,包含一个元素表,每个元素均包含一个Name 一个IDID是提供给程序使用的,就是用户的选择项。 

动作:Action.xml

<?xml version="1.0" encoding="utf-8"?>
<Root Type="Scene">
  <ActionList>
    <Action ClassName="PlayMusic" Name="PlayMusic" CHName="播放音乐(PlayMusic)"/>
    <Action ClassName="TMoveTo" Name="MoveTo" CHName="移动到(MoveTo)"/>
    <Action ClassName="TMoveBy" Name="MoveBy" CHName="移动(MoveBy)"/>
    <Action ClassName="TRotateTo" Name="RotateTo" CHName="旋转到(RotateTo)"/>
    <Action ClassName="TRotateBy" Name="RotateBy" CHName="旋转(RotateBy)"/>
    <Action ClassName="TScaleTo" Name="ScaleTo" CHName="缩放到(ScaleTo)"/>
    <Action ClassName="TScaleBy" Name="ScaleBy" CHName="缩放(ScaleBy)"/>
    <Action ClassName="TSkewTo" Name="SkewTo" CHName="歪曲到(SkewTo)"/>
    <Action ClassName="TSkewBy" Name="SkewBy" CHName="歪曲(SkewBy)"/>
    <Action ClassName="ArmaturePlayAction" Name="ArmaturePlayAction" CHName="骨骼播放动画(ArmaturePlayAction)"/>
    <Action ClassName="SequenceMoveTo" Name="SequenceMoveTo" CHName="序列移动到(SequenceMoveTo)"/>
    <Action ClassName="JumpAction" Name="JumpAction" CHName="向上跳并旋转动作(JumpAction)"/>
    <Action ClassName="FallAction" Name="FallAction" CHName="向下坠并旋转动作(FallAction)"/>
    <Action ClassName="ChangeDoubleAttribute" Name="ChangeDoubleAttribute"  CHName="改变Double自定义属性(ChangeDoubleAttribute)"/>
    <Action ClassName="AddAccToPositionY" Name="AddAccToPositionY" CHName="增加加速度在Y坐标(AddAccToPositionY)"/>
    <Action ClassName="SequenceMoveToAndChangePositionY" Name="SequenceMoveToAndChangePositionY" CHName="序列移动同时Y坐标随机(SequenceMoveToAndChangePositionY)"/>
    <Action ClassName="SetNodeVisible" Name="SetNodeVisible" CHName="设置节点隐藏显示(SetNodeVisible)"/>
    <Action ClassName="PlayUIAnimation" Name="PlayUIAnimation" CHName="播放UI动画"/>
    <Action ClassName="StopAllActions" Name="StopAllActions" CHName="停止所有动画"/>
    <Action ClassName="TriggerState" Name="TriggerState" CHName="触发器状态(TriggerState)"/>
  </ActionList>
</Root>

同样的,动作也有属性扩充,所以添加自己的动作的时候也要添加扩充文件,扩充文件主要是设置我们希望获取的属性:

  在xml中配置的所有,都是直接用于编辑器显示的,当我们点击“生成”的时候,会生成程序代码,在代码中真正的对这触发器做响应处理。

这些类在如下文件中:

下一节介绍代码部分。

原文地址:https://www.cnblogs.com/gl5773477/p/3976046.html