I want to vibrate my device in one of my slider functionality, using cocos2d-x 3.2.
I followed some of reference links regarding this on the official forums but I'm still stumped.
Can anyone help me?
I want to vibrate my device in one of my slider functionality, using cocos2d-x 3.2.
I followed some of reference links regarding this on the official forums but I'm still stumped.
Can anyone help me?
On
There's supposed to be a working implementation of vibrate on iOS and Android on FenneX's github.
The header file is in Classes/FenneX/NativeWrappers/NativeUtility.h.
iOS implementation is in proj.ios_mac/ios/Misc/NativeUtility.mm
Android implemention have the C++ in proj.android/jni/NativeUtility.cpp
the Java somewhere in the src.
sourced from a forum post asking the same thing
On
Based on the code found in the forum post:
Classes/ folder where you just have the cpp function declared but not defined.proj.android/jni, in a .cpp file. I used proj.android/jni/hellocpp/main.cpp, which I believe is the default for a starter cocos2dxv3 project. In this method you call the method you've defined in your java, in this case it was defined as a static method on your Activity. The default was named `AppActivity.proj.android/AndroidManifest.xml, <uses-permission android:name="android.permission.VIBRATE"/>So, in one of your standard C++ headers in the Classes/ folder, add
//Classes/any_header.h
void vibrate(int milliseconds);
and in the associated .cpp file, call it:
//Classes/any_header.cpp
void get_hit()
{
vibrate(200);
}
In your proj.android/AndroidManifest.xml, you'll want to add that line
<manifest>
...
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest>
In a cpp file the jni will deal with, say proj.android/jni/hellocpp/main.cpp, you'll need to define the connection between the Java and C++ by calling the Java method:
//proj.android/jni/hellocpp/main.cpp
/* this function should already exist though */
void cocos_android_app_init (JNIEnv* env, jobject thiz) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
}
/* this is the new one you're adding, where org/cocos2dx/cpp/AppActivity
is the java file where your Activity with the vibrate static method is defined
*/
void vibrate(int milliseconds)
{
JniMethodInfo minfo;
CCAssert(JniHelper::getStaticMethodInfo(minfo, "org/cocos2dx/cpp/AppActivity", "vibrate", "(I)V"), "Function doesn't exist");
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, (jint)milliseconds);
minfo.env->DeleteLocalRef(minfo.classID);
}
finally, here's the .java file where the android call to vibrate is made. Mine was proj.android/src/org/cocos2dx/cpp/AppActivity.java
/* default imports */
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
/* imports needed for vibration stuff */
import android.os.Vibrator;
import android.content.Context;
import android.app.ActivityManager;
import android.util.Log;
import android.os.Bundle;
public class AppActivity extends Cocos2dxActivity {
private static volatile Cocos2dxActivity mainActivity;
public static void setMainActivity(Cocos2dxActivity activity)
{
mainActivity = activity;
}
public static Cocos2dxActivity getMainActivity()
{
if(mainActivity == null)
{
Log.w("MY_APP_NAME_GOES_HERE", "Warning : null main Activity");
}
return mainActivity;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AppActivity.setMainActivity(this);
}
public static void vibrate(int milliseconds)
{
Vibrator v = (Vibrator) getMainActivity().getSystemService(Context.VIBRATOR_SERVICE);
if (getMainActivity().getSystemService(Context.VIBRATOR_SERVICE) != null)
{
v.vibrate(milliseconds);
}
}
}
After Searching a lot , Finally i am done with my Vibrate Function in Cocos2d-x 3.2 for android.
There is one Vibrator Class.
Vibrator.h
Vibrator.cpp
=> in AndroidManifest.xml : add below permission .
=> in src/org/cocos2dx/lib/Cocos2dxHelper.java file : add below 2 methods
first import => import android.os.Vibrator;
then , add these 2 methods
Now, Whenever we want to use Vibrate Function we just have to include this Vibrator Class & Simply Calling it as below .
For Disabling it,
in won't work in Tab as it does not having Vibrator in it .