码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d-x CocoStudio中场景触发器(Trigger)的代码部分和触发器之间的互调

时间:2014-09-21 00:17:19      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   strong   

这节继上一篇触发器扩展,讲一下代码部分的实现。

事件:EventDef.h

  只有一个枚举,是对触发器事件的编号

#ifndef__EVENTDEF__ 
#define__EVENTDEF__ 
 
enum { 
 
TRIGGEREVENT_ENTERSCENE = 0, 
TRIGGEREVENT_LEAVESCENE, 
TRIGGEREVENT_INITSCENE, 
TRIGGEREVENT_UPDATESCENE, 
TRIGGEREVENT_TOUCHBEGAN, 
TRIGGEREVENT_TOUCHMOVED, 
TRIGGEREVENT_TOUCHENDED, 
TRIGGEREVENT_TOUCHCANCELLED, 
}; 
 
#endif 

在程序中使用sendEvent方法,就将cocos2d-x引擎的事件引入到了触发器系统中了 :

boolHelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) 
{ 
         sendEvent(TRIGGEREVENT_TOUCHBEGAN); 
         return true; 
}

 

条件:cons.h 和 cons.cpp

以ArmatureActionState为例:

ArmatureActionState.h

class ArmatureActionState : 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); 
private: 
     int _nTag; 
     std::string _comName; 
     std::string _aniname; 
     int _nState; 
     bool _bSuc; 
}; 

 

ArmatureActionState.cpp

void ArmatureActionState::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,这里通过key来做判断,得到我们想要的值value。

 

动作:act.h 和 act.cpp

以TMoveTo为例:

和“条件判断”的实现多数方法也是类似,但不同的是detect()替换为done()函数,在该函数里,我们做触发事件后的处理。

void TMoveTo::done() 
{ 
   do 
   { 
      CCNode*pNode = 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); 
}

 

最后,我们有时候可能会需要在某一个触发器结束的时候调用另一个触发器,那么我们只需要在当前触发器属性中添加一个“下一个触发器ID”的属性,用来获取下一个触发器。

bubuko.com,布布扣

同一场景中所有新建的触发器都会自动在名字末尾添加一个有序编号,那是触发器的编号ID,我们可以通过这个ID来取得指定的触发器。

bubuko.com,布布扣

void TMoveTo::done() 
{ 
   do 
   { 
      /* 调用另一个触发器,m_nextTriggerId为下一个触发器的ID */
      TriggerObj *obj = TriggerMng::getInstance()->getTriggerObj(m_nextTriggerId);
     obj->done();
   }while (0); 
}

 

参考链接:http://www.cocoachina.com/bbs/simple/?t194739.html

 

cocos2d-x CocoStudio中场景触发器(Trigger)的代码部分和触发器之间的互调

标签:style   blog   http   color   io   os   使用   ar   strong   

原文地址:http://www.cnblogs.com/gl5773477/p/3979660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!