【cocos2d-x从c++到js】13:回调函数2——JSCallbackWrapper
发布时间:2020-12-14 19:43:25 所属栏目:百科 来源:网络整理
导读:2014-01-26 16:14:18 标签: cocos2d-x js jsb 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。 http://www.jb51.cc/article/p-ylevbpbn-vm.html 上一篇我们讲了按键回调,这一次我们来说说各种逻
2014-01-26 16:14:18
标签:
cocos2d-x
js
jsb
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处、作者信息和本声明。否则将追究法律责任。
http://www.52php.cn/article/p-ylevbpbn-vm.html
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
|
// cc.CallFunc.create( func,this,[data])
// cc.CallFunc.create( func )
static
JSBool js_callFunc(JSContext *cx,uint32_t argc,jsval *vp)
{
if
(argc >= 1 && argc <= 3) {
jsval *argv = JS_ARGV(cx,vp);
std::shared_ptr<JSCallbackWrapper> tmpCobj(
new
JSCallbackWrapper());
tmpCobj->setJSCallbackFunc(argv[0]);
(argc >= 2) {
tmpCobj->setJSCallbackThis(argv[1]);
}
(argc == 3) {
tmpCobj->setJSExtraData(argv[2]);
}
CallFuncN *ret = CallFuncN::create([=](Node* sender){
jsval& jsvalThis = tmpCobj->getJSCallbackThis();
jsval& jsvalCallback = tmpCobj->getJSCallbackFunc();
jsval& jsvalExtraData = tmpCobj->getJSExtraData();
bool
hasExtraData = !JSVAL_IS_VOID(jsvalExtraData);
JSObject* thisObj = JSVAL_IS_VOID(jsvalThis) ? nullptr : JSVAL_TO_OBJECT(jsvalThis);
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::Node>(cx,sender);
jsval retval;
(jsvalCallback != JSVAL_VOID)
{
(hasExtraData)
{
jsval valArr[2];
valArr[0] = OBJECT_TO_JSVAL(proxy->obj);
valArr[1] = jsvalExtraData;
JS_AddValueRoot(cx,valArr);
JS_CallFunctionValue(cx,thisObj,jsvalCallback,2,valArr,&retval);
JS_RemoveValueRoot(cx,valArr);
}
else
{
jsval senderVal = OBJECT_TO_JSVAL(proxy->obj);
}
// I think the JSCallFuncWrapper isn't needed.
// Since an action will be run by a cc.Node,it will be released at the Node::cleanup.
// By James Chen
// JSCallFuncWrapper::setTargetForNativeNode(node,(JSCallFuncWrapper *)this);
});
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CallFunc>(cx,ret);
JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(proxy->obj));
JS_SetReservedSlot(proxy->obj,argv[0]);
(argc > 1) {
// if(argc == 3) {
// JS_SetReservedSlot(proxy->obj,argv[2]);
// }
// test->execute();
return
JS_TRUE;
}
JS_ReportError(cx,
"Invalid number of arguments"
);
JS_FALSE;
}
|
10
CallFuncN * CallFuncN::create(
std::function<
(Node*)> &func)
{
auto
ret =
CallFuncN();
(ret && ret->initWithFunction(func) ) {
ret->autorelease();
ret;
}
CC_SAFE_DELETE(ret);
nullptr;
}
5
CallFuncN::initWithFunction(
(Node *)> &func)
_functionN = func;
return
true
;
8
(_callFuncN) {
(_selectorTarget->*_callFuncN)(_target);
}
else
(_functionN) {
_functionN(_target);
}
}