creator手机震动jsb.device.vibrate

0 阅读1分钟

creator3.8.7

bool js_register_cc_Device(se::Object* obj) {
    auto* cls = se::Class::create("Device", obj, nullptr, nullptr); 
    
    cls->defineStaticProperty("__isJSB", se::Value(true), se::PropertyAttribute::READ_ONLY | se::PropertyAttribute::DONT_ENUM | se::PropertyAttribute::DONT_DELETE);
    
    
    
    cls->defineStaticFunction("getDPI", _SE(js_cc_Device_getDPI_static)); 
    cls->defineStaticFunction("getDevicePixelRatio", _SE(js_cc_Device_getDevicePixelRatio_static)); 
    cls->defineStaticFunction("setAccelerometerEnabled", _SE(js_cc_Device_setAccelerometerEnabled_static)); 
    cls->defineStaticFunction("setAccelerometerInterval", _SE(js_cc_Device_setAccelerometerInterval_static)); 
    cls->defineStaticFunction("getDeviceOrientation", _SE(js_cc_Device_getDeviceOrientation_static)); 
    cls->defineStaticFunction("getDeviceModel", _SE(js_cc_Device_getDeviceModel_static)); 
    cls->defineStaticFunction("setKeepScreenOn", _SE(js_cc_Device_setKeepScreenOn_static)); 
    cls->defineStaticFunction("vibrate", _SE(js_cc_Device_vibrate_static)); // 震动函数绑定
    cls->defineStaticFunction("getBatteryLevel", _SE(js_cc_Device_getBatteryLevel_static)); 
    cls->defineStaticFunction("getNetworkType", _SE(js_cc_Device_getNetworkType_static)); 
    cls->defineStaticFunction("getSafeAreaEdge", _SE(js_cc_Device_getSafeAreaEdge_static)); 
    
    
    cls->defineFinalizeFunction(_SE(js_delete_cc_Device));
    
    
    cls->install();
    JSBClassType::registerClass<cc::Device>(cls);
    
    __jsb_cc_Device_proto = cls->getProto();
    __jsb_cc_Device_class = cls;
    se::ScriptEngine::getInstance()->clearException();
    return true;
}

注册所有的引擎模块


bool register_all_engine(se::Object* obj) {
    // Get the ns
    se::Value nsVal;
    if (!obj->getProperty("jsb", &nsVal, true))
    {
        se::HandleObject jsobj(se::Object::createPlainObject());
        nsVal.setObject(jsobj);
        obj->setProperty("jsb", nsVal); // 全局变量jsb
    }
    se::Object* ns = nsVal.toObject();
    /* Register classes */
    js_register_cc_Vec2(ns); 
    js_register_cc_Color(ns); 
    js_register_cc_Vec3(ns); 
    js_register_cc_Vec4(ns); 
    js_register_cc_Mat3(ns); 
    js_register_cc_Mat4(ns); 
    js_register_cc_Quaternion(ns); 
    js_register_cc_CCObject(ns); 
    js_register_cc_JSBNativeDataHolder(ns); 
    js_register_cc_ICanvasGradient(ns); 
    js_register_cc_ICanvasRenderingContext2D(ns); 
    js_register_cc_CanvasGradient(ns); 
    js_register_cc_CanvasRenderingContext2D(ns); 
    js_register_cc_Device(ns); 
    js_register_cc_ISystemWindow(ns); 
    js_register_cc_ISystemWindowInfo(ns); 
    js_register_cc_ISystemWindowManager(ns); 
    js_register_cc_FileUtils(ns); 
    js_register_cc_SAXParser(ns); 
#if CC_USE_DEBUG_RENDERER
    js_register_cc_DebugTextInfo(ns); 
#endif // CC_USE_DEBUG_RENDERER
#if CC_USE_DEBUG_RENDERER
    js_register_cc_DebugRenderer(ns); 
#endif // CC_USE_DEBUG_RENDERER
    
    /* Register global variables & global functions */
    
    
    
    return true;
}

从以上的绑定代码中可以看到,正确的调用方式应该是jsb.Device.vibrate,实际测试发现这样调用是正确的。

但是为啥jsb.device.vibrate也能使用呢?adapter做了转换。

image.png

注意,Device实际只绑定到了jsb变量上,native变量不存在device