How to implement vibration into the game?

Hello Devs,

Well i’ve searched some of the posts and most of them are very old
and some might not be working also…!!

So, here what i want to ask
I don’t seem cocos2d-x to have vibration to implement in games
I don’t know the cause, why they are not implementing this … !!
as this issue was raised in the late 2011 itself

But can’t wait for that… :stuck_out_tongue:

This post might be working, but it’s for a very older version of cocos2d-x
also… i dont understand this post much

I use android 19 ndk r9d cocos2d-x 3.2

Can anyone give me a detailed view that how can i have vibration in my game ??
any help is greatly appreciated. :smile:

Hello,

I implemented that on Android, it’s rather straightforward when you know JNI. You can see the implementation on my framework: https://github.com/FenneX/FenneX
The header is in projects/Classes/FenneX/NativeWrappers/NativeUtility.h
There is a .cpp file in projects/proj.android/jni/NativeUtility.cpp for the Android implementation
There is .java file in projects/proj.android/java/src/com/fennex/modules/NativeUtility.java

But there are a lot of limits that make vibration not really useful:

  • all tablets I tried don’t have a vibrator, so if it is a required feature you won’t be on tablets
  • there is a small delay before the device actually vibrate, which makes precise vibration a bit hard to do

The framework is for cocos2d-x 2.2, but those particular files work for v3.2, though you may have deprecated warnings.

1 Like

@Fradow thanks you very much :smile:

i will dive into the code directly, and not the problem with the deprecation issues.
i will handle it.

Hmm, so that’s a real issue.
If the tablets dont have vibrators then it’s of no use at all
we are implementing this thing for a tablet based learning solution
which has games at the end of each chapter.

so so let me cross check, if the tablets we are pushing has vibrators or not.

Anyway thank you very much for your support… :smile:

Don’t forget to add the VIBRATE permission.

heii, have you done this using Cocos2d-x 3.2 version ??!!!

My Solution : http://stackoverflow.com/questions/28018919/how-to-add-vibration-to-cocos2d-x-3-2-on-android-and-ios-devices/28688814#28688814

@shaqir29

Hi,

I tried out your solution. I made fresh Vibrator.h and Vibrator.cpp files and removed ever pre-written code from them. And then added Vibrator.cpp to Android.mk

In the Vibrator.h itself, I am having following error at #endif which is the last line of your code of Vibrator.h
because of unmatched #endif. But I managed it by adding #ifndef VIBRATOR_H_ and #define VIBRATOR_H_ where #ifndef matches with previously unmatched #endif.

And the real error was in Vibrator.cpp

The positions/lines where error occurred in both Vibrate() and CancelVibrate():

1) JniMethodInfo t;
Error:
Multiple markers at this line
- expected ‘;’ before ‘t’
- ‘JniMethodInfo’ was not declared in this scope
- suggested alternative:

2) if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, “vibrate”, “(I)V”))
Error:
Multiple markers at this line
- ‘JniHelper’ has not been declared
- ‘t’ was not declared in this scope

What to do?

Thanxx :smile:

@pabitrapadhy

Sorry for reviving back the post.
Have you got the solution for the same cocos2d-x v3.2 ?

I tried solition as recently given above:
I got errors as mentioned in my last pozt!!

Any possible way?

Thnaxx :smiley:

@code_game_chef: Very First thing , have you included jni & jnihelper properly as i have shown in my answer ?? & are these errors on android (eclipse) side ??

See , i have not received any this kind of error in my code … i have used it in cocos2d-x 3.2 on my mac 10.9.5 with latest Xcode 6 . with adt-bundle-mac-x86_64-20140702 SDK , with NDK_r9d . it jus working Fine .

if you can tell me more specific scenario then i might help you. but right now , i am not getting your issue.

Thank you .

@shaqir29

Oh yes, in fact I have copy pasted your whole code. So, there isn’t any problem in inclusion.
In fact, I added the inclusion additionally to Vibrator.cpp as follows:

#include “platform/android/jni/JniHelper.h”
#include < jni.h>
#include < android/log.h>

But the same problems exists

You told that I’ve not stated my details specifically. But I’ve already listed the detailed list of errors as in my last to last post… Also, I’ve quoted them along in this post.

I’ve also tried using:

Can you please help with this if I am not getting it from your way?

Thanxx a lot…

Also, along with this…

You told to add the following in Vibrator.cpp

if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "vibrate", "(I)V"))

What this CLASS_NAME is supposed to be?
First, I let it remain as CLASS_NAME

and then I changed it to Vibrator but nothing working…

But still the problem isn’t coming at CLASS_NAME but JniHelper even when I’ve included
#include < jni.h>
#include < android/log.h>
#include “platform/android/jni/JniHelper.h”

explicitly in Vibrator.cpp even though the same is included through your code in Vibrator.h

Thanxx

Hey , its Fine , I think i missed to inform you …

#define CLASS_NAME “org/cocos2dx/lib/Cocos2dxHelper”

thats the CLASS_NAME .

=> in your AndroidManifest.xml : add below permission .

   <uses-permission android : name = "android.permission.VIBRATE" /> 

=> in your src/org/cocos2dx/lib/Cocos2dxHelper.java file : add below 2 methods

first import => import android.os.Vibrator;

then , add these 2 methods

public static void vibrate ( int vibrateTime)
{
Vibrator vib = (Vibrator) sActivity.getSystemService (Service.VIBRATOR_SERVICE);
vib.vibrate (vibrateTime);
}

public  static  void  cancelVibrate () 

{
Vibrator vib = (Vibrator) sActivity.getSystemService (Service.VIBRATOR_SERVICE);
vib.cancel ();
}

this might solve your problem … still ,tell me if you getting problem …

@shaqir29

Kindly check your PM :smile:

Thanxx

Also, I forget to see that you’ve already given CLASS_NAME in .h file as
#define CLASS_NAME “org/cocos2dx/lib/Cocos2dxHelper”

But, I did it again in .cpp file as I think it is important.