【教程】“回调特性”的使用教程(C++语言)

【新功能】“回调特性”的使用教程: 
 
简介:Cocos Studio2.0.5版本新增回调特性功能。
 
现在可以直接在编辑器里边指定某个控件的事件的处理函数。  
不再需要一个一个去get控件啦。也不再需要因为修改了控件名或者其他属性后,就得去改程序啦。  
这些,都可以交给策划了。                             
 
 
ok,以下是正文  
 
1. 创建文件,修改自定义类名称。  
点击根节点  

   
 
 
 
在属性栏中修改自定义类类名,如 :MyClass  

   
 
2. 加入UI控件修改回调方法。  
选中想要设置回调特性的控件,进入高级属性,修改回调方法和对应回调的名称。  
如: Touch 方法,名称为 onTouch  

   
 
然后发布资源  
3. C++中,创建一个自己的自定义类。  
这个类必须遵循如下要求:  
a. 继承 WidgetCallBackHandlerProtocol  Node( 或其他继承自 Node 的类 )  
b. 重写如下接口:  
onLocateTouchCallback  
onLocateClickCallback  
onLocateEventCallback  
返回对 Touch Click Event 三种事件的处理函数。(可以只重写你使用到的回调类型)。  
如:  
//.h file  
 
#ifndef __TestCpp__MyClass__  
#define __TestCpp__MyClass__  
#include "cocos2d.h"  
#include "cocostudio/CocoStudio.h"  
#include "cocostudio/WidgetCallBackHandlerProtocol.h"  
class MyClass : public cocos2d::Node, public cocostudio::WidgetCallBackHandlerProtocol  
{  
public:  
    CREATE_FUNC(MyClass)  
MyClass();  
virtual cocos2d::ui::Widget::ccWidgetTouchCallback  
onLocateTouchCallback(const std::string &callBackName);  
virtual cocos2d::ui::Widget::ccWidgetClickCallback  
onLocateClickCallback(const std::string &callBackName);  
virtual cocos2d::ui::Widget::ccWidgetEventCallback  
onLocateEventCallback(const std::string &callBackName);  
void onTouch(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);  
void onClick(cocos2d::Ref* sender);  
void onEvent(cocos2d::Ref* sender, int eventType);  
private:  
std::vector<std::string> _touchTypes;  
std::string _click;  
std::vector<std::string> _eventTypes;  
};  
//.cpp file  
#include "MyClass.h"  
 
 
#include "ui/UIText.h"  
 
 
USING_NS_CC;  
using namespace std;  
using namespace cocos2d::ui;  
 
 
MyClass::MyClass()  
{}  
 
 
Widget::ccWidgetTouchCallback MyClass::onLocateTouchCallback(const string &callBackName)  
{  
if (callBackName == "onTouch")//判断事件名,返回对应的函数。下同  
{  
return CC_CALLBACK_2(MyClass::onTouch, this);  
}  
return nullptr;  
}  
 
 
Widget::ccWidgetClickCallback MyClass::onLocateClickCallback(const string &callBackName)  
{  
if (callBackName == "onClick")  
{  
return CC_CALLBACK_1(MyClass::onClick, this);  
}  
return nullptr;  
}  
 
 
Widget::ccWidgetEventCallback MyClass::onLocateEventCallback(const string &callBackName)  
{  
if (callBackName == "onEvent")  
{  
return CC_CALLBACK_2(MyClass::onEvent, this);  
}  
return nullptr;  
}  
 
 
void MyClass::onTouch(cocos2d::Ref* object, cocos2d::ui::Widget::TouchEventType type)  
{  
CCLOG("onTouch");  
}  
 
 
void MyClass::onClick(cocos2d::Ref* sender)  
{  
CCLOG("onClick");  
}  
 
 
void MyClass::onEvent(cocos2d::Ref* sender, int eventType)  
{  
CCLOG("onEvent");  
}  
 
4. 为第3步编写的类创建工厂类  
这个类必须继承 cocostudio::NodeReader ,并重写如下三个接口  
getInstance  ——   返回工厂类的单例  
purge  ——   销毁工厂类  
createNodeWithFlatBuffers  ——   创建第 3 步编写的类,并调用 setPropsWithFlatBuffers  
如:  
//.h file  
 
 
 
#ifndef __cocos2d_libs__MyClassReader__
#define __cocos2d_libs__MyClassReader__
#include "cocos2d.h"
#include "cocostudio/CocosStudioExport.h"
#include "cocostudio/WidgetReader/NodeReader/NodeReader.h"
class MyClassReader : public cocostudio::NodeReader
{       
public:
MyClassReader() {};
    ~MyClassReader() {};   
static MyClassReadergetInstance();
static void purge();
cocos2d::NodecreateNodeWithFlatBuffers(const flatbuffers::TablenodeOptions);
};
#endif /*defined(__cocos2d_libs__MyClassReader__) */ 
//.cpp file
#include "MyClassReader.h"
#include "MyClass.h"
USING_NS_CC;
static MyClassReader_instanceMyClassReader = nullptr;
MyClassReaderMyClassReader::getInstance()
{
if (!_instanceMyClassReader)
    {
_instanceMyClassReader  =  new   MyClassReader ();
    }
return _instanceMyClassReader;
}
void MyClassReader::purge()
{
CC_SAFE_DELETE(_instanceMyClassReader);
}
NodeMyClassReader::createNodeWithFlatBuffers(const flatbuffers::Table *nodeOptions)
{
MyClassnode = MyClass::create();
setPropsWithFlatBuffers(nodenodeOptions);
return node;
}  
 
5. 在加载节点之前注册这个接口到CSLoader  
CSLoaderinstance = CSLoader::getInstance();
instance -> registReaderObject ( " MyClassReader" , ( ObjectFactory :: Instance ) MyClassReader :: getInstance );
注意第一个参数必须是第一步填写的自定义类名加“Reader ”如上述的"MyClassReader" 
6.  使用CreateNode
加载你的节点。  
 
 
 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!注意:!!!  
你的自定义类的create已经委托给工厂类,只要你注册工厂类的时候没写错,工厂类会在createNode里边create你的自定义类。 
所以不需要你自己再create自定义类。
最终createNode返回的就是你的自定义类。  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<span style= "\"display:none\"" > o#qdgZ </span> 
<span style= "\"display:none\"" > Sz0PZtJ </span> 
#ifndef __TestCpp__MyClass__<span style="\"display:none\""> -@N-i$!;J </span> 
#define __TestCpp__MyClass__<span style="\"display:none\""> 6\"-$WUlg </span> 
#include "cocos2d.h"<span style="\"display:none\""> 7By7F:[\0b </span> 
#include "cocostudio/CocoStudio.h"<span style="\"display:none\""> u]*7\",R
uU </span> 
#include "cocostudio/WidgetCallBackHandlerProtocol.h"<span style="\"display:none\""> 2C
\0S9v </span> 
class MyClass : public cocos2d::Node, public cocostudio::WidgetCallBackHandlerProtocol<span style= "\"display:none\"" > @  m`C%7< </span> 
{<span style= "\"display:none\"" > E*'Y\0xI </span> 
public :<span style= "\"display:none\"" > RC_Pj) </span> 
       CREATE_FUNC(MyClass)<span style= "\"display:none\"" > j97+'AKX </span> 
MyClass();<span style= "\"display:none\"" > #cY[c1cNv </span> 
virtual cocos2d::ui::Widget::ccWidgetTouchCallback onLocateTouchCallback( const std::string &callBackName);<span style= "\"display:none\"" > :C5w5
Vnj </span> 
virtual cocos2d::ui::Widget::ccWidgetClickCallback onLocateClickCallback( const std::string &callBackName);<span style= "\"display:none\"" > PD&e6;rj; </span> 
         virtual cocos2d::ui::Widget::ccWidgetEventCallback onLocateEventCallback( const std::string &callBackName);<span style= "\"display:none\"" > NM. e4 </span> 
         void onTouch(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);<span style= "\"display:none\"" > NpVL;6?7T </span> 
         void onClick(cocos2d::Ref* sender);<span style= "\"display:none\"" > oG,>Pk </span> 
         void onEvent(cocos2d::Ref* sender, int eventType);<span style= "\"display:none\"" > ?1=.scmgDG </span> 
private :<span style= "\"display:none\"" > !U`4 </span> 
std::vector<std::string> _touchTypes;<span style= "\"display:none\"" > J^+w]2`S </span> 
std::string _click;<span style= "\"display:none\"" > 971=OEyq* </span> 
std::vector<std::string> _eventTypes;<span style= "\"display:none\"" > ?b_E\8'q] </span> 
};<span style= "\"display:none\"" > A'EI1_3{ </span> 
//.cpp file<span style="\"display:none\""> 4?pb!@l </span> 
#include "MyClass.h"<span style="\"display:none\""> t'_EcYNS  </span> 
<span style= "\"display:none\"" > *;Kp\"j </span> 
<span style= "\"display:none\"" > dS1HA>c)O </span> 
#include "ui/UIText.h"<span style="\"display:none\"">
UBd+,]\"f </span> 
<span style= "\"display:none\"" > S*l/
Sa@ </span> 
<span style= "\"display:none\"" > h8V*$ </span> 
USING_NS_CC;<span style= "\"display:none\"" > ANm@$xO* </span> 
using namespace std;<span style= "\"display:none\"" > YJ~<pH </span> 
using namespace cocos2d::ui;<span style= "\"display:none\"" > JC'3x9_<z </span> 
<span style= "\"display:none\"" > QQAEG#.5 </span> 
<span style= "\"display:none\"" > 2$JZ(qnN </span> 
MyClass::MyClass()<span style= "\"display:none\"" > I\"&cr>\ </span> 
{}<span style= "\"display:none\"" > Z}O]pm>=G </span> 
<span style= "\"display:none\"" > C^q|(G) </span> 
<span style= "\"display:none\"" > MZ3 8=nJ </span> 
Widget::ccWidgetTouchCallback MyClass::onLocateTouchCallback( const string &callBackName)<span style= "\"display:none\"" > ~<k>07 </span> 
{<span style= "\"display:none\"" > aR2N,<Cp5 </span> 
if (callBackName == "onTouch" ) //判断事件名,返回对应的函数。下同<span style="\"display:none\""> W*\0LC3B^ </span> 
{<span style= "\"display:none\"" > !gI0\"p? </span> 
return CC_CALLBACK_2(MyClass::onTouch, this );<span style= "\"display:none\"" > ?e9tnk3 </span> 
}<span style= "\"display:none\"" > c =m#MMc) </span> 
return nullptr ;<span style= "\"display:none\"" > W'6DwV| </span> 
}<span style= "\"display:none\"" > 8L[+$g` </span> 
<span style= "\"display:none\"" > hk
!=ZE3 </span> 
<span style= "\"display:none\"" > 56C8)? </span> 
Widget::ccWidgetClickCallback MyClass::onLocateClickCallback( const string &callBackName)<span style= "\"display:none\"" > vU%o5y
: </span> 
{<span style= "\"display:none\"" >
#ed|0 </span> 
if (callBackName == "onClick" )<span style= "\"display:none\"" > k\0_t|)
J </span> 
{<span style= "\"display:none\"" > V_3oAu54s{ </span> 
return CC_CALLBACK_1(MyClass::onClick, this );<span style= "\"display:none\"" > D:k<
, { </span> 
}<span style= "\"display:none\"" > \"I56l2dxd </span> 
return nullptr ;<span style= "\"display:none\"" > 8i;1JA </span> 
}<span style= "\"display:none\"" > :s_o'8z7L </span> 
<span style= "\"display:none\"" > | >}CoR7 </span> 
<span style= "\"display:none\"" > 8YZ9 </span> 
Widget::ccWidgetEventCallback MyClass::onLocateEventCallback( const string &callBackName)<span style= "\"display:none\"" > 3?E7\\/R </span> 
{<span style= "\"display:none\"" > q&=z^Ln!G </span> 
if (callBackName == "onEvent" )<span style= "\"display:none\"" > bofI0f}5. </span> 
{<span style= "\"display:none\"" > ]2u
</span> 
return CC_CALLBACK_2(MyClass::onEvent, this );<span style= "\"display:none\"" > !v2/sq$G </span> 
}<span style= "\"display:none\"" > |2'WSAWG </span> 
return nullptr ;<span style= "\"display:none\"" > J>R $K </span> 
}<span style= "\"display:none\"" > ql9n`?Q  </span> 
<span style= "\"display:none\"" >
gI7*zR4D </span> 
<span style= "\"display:none\"" > w*{{bISw| </span> 
void MyClass::onTouch(cocos2d::Ref* object, cocos2d::ui::Widget::TouchEventType type)<span style= "\"display:none\"" > bEF2- FO </span> 
{<span style= "\"display:none\"" > l]wfL;u </span> 
CCLOG( "onTouch" );<span style= "\"display:none\"" > bF9.k </span> 
}<span style= "\"display:none\"" > q=^;lWs4 </span> 
<span style= "\"display:none\"" > L%H\|>
k` </span> 
<span style= "\"display:none\"" > \"PMJh\03q </span> 
void MyClass::onClick(cocos2d::Ref* sender)<span style= "\"display:none\"" > 'LoWp} f9 </span> 
{<span style= "\"display:none\"" > $j,$O>V  </span> 
CCLOG( "onClick" );<span style= "\"display:none\"" > `Ku:%~$/ </span> 
}<span style= "\"display:none\"" > T=/c0#Q|q </span> 
<span style= "\"display:none\"" > gjsks
(x </span> 
<span style= "\"display:none\"" > iUz?mt;k </span> 
void MyClass::onEvent(cocos2d::Ref* sender, int eventType)<span style= "\"display:none\"" > I3
6@x`f </span> 
{<span style= "\"display:none\"" > b
B#QIXY/L </span> 
CCLOG( "onEvent" );<span style= "\"display:none\"" >  b81^756 </span> 
}<span style= "\"display:none\"" > Yv=L'0K& </span> 


FROM: http://www.cocoachina.com/bbs/read.php?tid=274210
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值