cocos creator 实现手机震动的效果

目前需要wx, android,ios三个平台发布

android 平台下:

  1. 将项目打包发布形成build文件夹,用Android studio 打开如下目录:***\build\jsb-link\frameworks\runtime-src\proj.android-studio

  2. 在class APPActivity() 下添加静态方法:
    cocos creator 实现手机震动的效果_第1张图片

//这里要注意cocos creator修改了之前的AppActivity的代码:在开头添加建立一个app对象
 public static AppActivity app = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
        if (!isTaskRoot()) {
            // Android launched another instance of the root activity into an existing task
            //  so just quietly finish and go away, dropping the user back into the activity
            //  at the top of the stack (ie: the last state of this task)
            // Don't need to finish it again since it's finished in super.onCreate .
            return;
        }
        // DO OTHER INITIALIZATION BELOW
        
        SDKWrapper.getInstance().init(this);
        CAAgent.enableDebug(false);
        app = this;
    }
    
public static void vibrate(){
            Vibrator vib = (Vibrator) app.getSystemService(Service.VIBRATOR_SERVICE);
            vib.vibrate(1000);
    }
  1. 添加权限:
    cocos creator 实现手机震动的效果_第2张图片
    打开manifests, 添加:

  2. 反射该方法:参考官网

//写入全局中,后面调用即可。
vibration: function () {
        if (cc.sys.os === cc.sys.OS_IOS)  //调用苹果的方法;
        else if (cc.sys.os === cc.sys.OS_ANDROID) jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "vibrate", "()V");
        //()V的意思具体参考官网的文档
    },

ios平台下:

后面再补充。
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
vib.vibrate(milliseconds); 有个VIBRATOR_SERVICE可以用```

你可能感兴趣的:(cocos,creator)