Continuing the discussion from How to add vibration to android game in cocos2d js?:
Thanks for the code, could you also provide the C++ implementation of vibration and if possible with the ability to pass the vibration time to the function?
Continuing the discussion from How to add vibration to android game in cocos2d js?:
Thanks for the code, could you also provide the C++ implementation of vibration and if possible with the ability to pass the vibration time to the function?
in Java
public static void callVibration(int time) {
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(time);
}
in Js
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "callVibration", "(I;)V", 400);
And about C++ is out of my experience, sorry
Thanks for the functions. I found the C++ implementation -
Include these -
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
And this is the function -
JniMethodInfo methodInfo;
if (JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/cpp/AppActivity", "callVibration", "(I;)V")) {
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID,400);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
But when I try to compile it, I get the errors -
[javac] proj.android\src\org\cocos2dx\cpp\AppActivity.java :34: error: cannot find symbol [javac] protected void onCreate(Bundle savedState) { [javac] ^ [javac] symbol: class Bundle [javac] location: class AppActivity [javac] proj.android\src\org\cocos2dx\cpp\AppActivity.java:39: error: cannot find symbol [javac] Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE); [javac] ^ [javac] symbol: variable Context [javac] location: class AppActivity [javac] 2 errors
I used the java code that you gave -
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Vibrator;
public class AppActivity extends Cocos2dxActivity {
private static AppActivity app = null;
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
app = this;
}
public static void callVibration(int time) {
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(time);
}
}
How can I fix this error?
try “(I;)V” -> “(I)V”
Thanks, that was also a problem I didn’t notice
I also fixed the java error by -
import android.os.Bundle;
import android.content.Context;
Hi, what about for iOS? Could you also provide the C++ implementation for iOS?
It’d be great to get this working for WP too 
Vibration should be wrapped within the Cocos2d-x framework so we don’t have to hack a workaround for each platform.
Does ios app store (Apple) allowed custom vibration ??? i fear they might not … please share your thoughts on this.
@shaqir29 No ios app store doesnt allow custom vibration. Also you have to provide vibrate on/off btn inside game to pass your game successfully.