Some methods for using JNI have changed in the 1.0.1-x-0.11.0 version of cocos2dx. So I decided to adjust the methods to the file SimpleAudioEngineJni.cpp. This was the only file I modified to get vibration to work again. The rest of the files I just copied over, but I will give the instructions on how to get Vibration on your game in this post. Most of it will be from the another post I wrote the instructions on.
I edited 7 files in total. Here are the instructions of what to do:
- You must edit the AndroidManifest.xml file which is located in “/android/”
Add the following line:
This will allow your app to use vibration.
- I modified 6 other files which are in the zip file I attached to this post. Here’s where each file goes:
2.1 Both Cocos2dxActivity.java and Cocos2dxsound.java are generated in your project directory everytime you run the create-android-project.bat file. You must place Cocos2dxActivity.java and Cocos2dxsound.java and into:
The modifications I added were adding 3 methods; vibrate(long time), vibrateWithPattern(long pattern, int repeat), cancelVibrate().
2.2 Next two files are SimpleAudioEngineJni.h and SimpleAudioEngineJni.cpp. These two files go into:
The modifications are the addition of 3 new methods; vibrateJNI(long long time), vibrateWithPatternJNI(long long pattern, int repeat), cancelVibrateJNI().
2.3 The next file is SimpleAudioEngine.h. This goes into:
The modifications are declaring the 3 new methods; vibrate(long long time), vibrateWithPattern(long long pattern, int repeat), cancelVibrate()
2.4 The last file is SimpleAudioEngine.cpp. This goes into:
The modifications are adding the 3 new methods; vibrate(long long time), vibrateWithPattern(long long pattern, int repeat), cancelVibrate().
- To add vibration to your game, all you need to do is call the methods like so:
3.1 Vibration with the variable time which is a long type integer to determine how long the vibration lasts for:
The variable time is a measurement of milliseconds.
3.2 VibrationWithPattern with two variables; Pattern which is a array of long integers which is measured in milliseconds, and Repeat which is an interger:
Pattern tells the vibrator when to turn on or off. The first integer in the array tells the vibrator how long to wait before turning on and the next interger tells the vibrator how long to stay on before switching off. Example would be which would wait 1 millisecond before switching on and will last 2 seconds before switching off. The array can be as big as you want it. The sequence will be as described e.g. . Repeat basically tells the vibrator how long to repeat this pattern, or~~1 if you don’t want it to repeat.
3.2 cancelVibrate turns off the vibrator:
The methods were created with intention of keeping the same structure of the vibrate methods from the android sdk: http://developer.android.com/reference/android/os/Vibrator.html.
Vibrate.zip (10.9 KB)
Vibrate.zip (12.3 KB)
Vibrate.zip (12.3 KB)