【cocos2d-x从c++到js】15:傀儡构造函数
发布时间:2020-12-14 19:43:40 所属栏目:百科 来源:网络整理
导读:2014-02-11 00:26:07 标签: cocos2d-x js jsb 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。 http://www.jb51.cc/article/p-ekyteoji-vm.html 上篇我们以Sprite为例,分析了注册函数。但其中我
2014-02-11 00:26:07
标签:
cocos2d-x
js
jsb
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处、作者信息和本声明。否则将追究法律责任。
http://www.52php.cn/article/p-ekyteoji-vm.html
6
7
8
9
|
jsb_cocos2d_Sprite_prototype = JS_InitClass(
cx,global,
jsb_cocos2d_Node_prototype,
jsb_cocos2d_Sprite_class,
dummy_constructor<cocos2d::Sprite>,
// no constructor
properties,
funcs,
NULL,0)!important; background:none!important">// no static properties
st_funcs);
|
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<
class
T>
static
JSBool dummy_constructor(JSContext *cx,jsval *vp) {
JS::RootedValue initializing(cx);
JSBool isNewValid = JS_TRUE;
JSObject* global = ScriptingCore::getInstance()->getGlobalObject();
isNewValid = JS_GetProperty(cx,
"initializing"
,&initializing) && JSVAL_TO_BOOLEAN(initializing);
if
(isNewValid)
{
TypeTest<T> t;
js_type_class_t *typeClass = nullptr;
std::string typeName = t.s_name();
auto
typeMapIter = _js_global_type_map.find(typeName);
CCASSERT(typeMapIter != _js_global_type_map.end(),monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; background:none!important">"Can't find the class type!"
);
typeClass = typeMapIter->second;
CCASSERT(typeClass,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; background:none!important">"The value is null."
);
JSObject *_tmp = JS_NewObject(cx,typeClass->jsclass,typeClass->proto,typeClass->parentProto);
JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(_tmp));
return
JS_TRUE;
}
JS_ReportError(cx,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; background:none!important">"Don't use `new cc.XXX`,please use `cc.XXX.create` instead! "
);
JS_FALSE;
}
12
DERIVED >
TypeTest
{
public
:
static
const
char
* s_name()
{
// return id unique for DERIVED
// ALWAYS VALID BUT STRING,NOT INT - BUT VALID AND CROSS-PLATFORM/CROSS-VERSION COMPATBLE
// AS FAR AS YOU KEEP THE CLASS NAME
return
typeid
( DERIVED ).name();
}
};