1、在proj.android这个目录下
AndroidManifest.xml 文件,给app 增加震动权限
<uses-permission android : name = "android.permission.VIBRATE" />
2、 修改android 项目源文件src/org/cocos2dx/lib/Cocos2dxSound.java增加震动方法,调用android底层振动器
@param time 震动时间 */
public void vibrate( long time) {
Vibratorv = (Vibrator) mContext .getSystemService(Context. VIBRATOR_SERVICE);
v.vibrate(time);
}
@param pattern 震动时间数组 EG:{500,200,500,300}* @param repeat 重复次数 */
public void vibrateWithPattern( long [] pattern, int repeat) {
Vibratorv = (Vibrator) mContext .getSystemService(Context. VIBRATOR_SERVICE);
v.vibrate(pattern,repeat);
}
/*** 取消震动 */
public void cancelVibrate() {
Vibratorv = (Vibrator) mContext .getSystemService(Context. VIBRATOR_SERVICE);
v.cancel();
}
3. 至此,java 项目中的修改就OK啦,下面就是在Cocos2d-x中增加JNI方法让C++去掉用Java的震动方法就OK啦!
3.1. 修改 libCocosDenshion 项目中cocos2d-x\CocosDenshion\android\jni文件夹下的SimpleAudioEngineJni.h和SimpleAudioEngineJni.cpp方法
在SimpleAudioEngineJni.h 中添加
#ifndef __SIMPLE_AUDIO_ENGINE_JNI__
#define __SIMPLE_AUDIO_ENGINE_JNI__
#include
extern "C"
{
......
// add thesementhod
extern void vibrateJNI(long long time);
extern void vibrateWithPatternJNI(long long pattern[], intrepeat);
extern void cancelVibrateJNI();
}
#endif // __SIMPLE_AUDIO_ENGINE_JNI__
在SimpleAudioEngineJni.cpp 中添加
// add these jni menthod
void vibrateJNI(long long time)
{
JniMethodInfomethodInfo;
if (! getStaticMethodInfo(methodInfo, "vibrate", "(J)V"))
{
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, time);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
void vibrateWithPatternJNI(long long pattern[], int repeat)
{
JniMethodInfomethodInfo;
if (! getStaticMethodInfo(methodInfo, "vibrateWithPattern", "([JI)V"))
{
return;
}
int elements = sizeof(pattern);
jlongArrayjLongArray = methodInfo.env->NewLongArray(elements);
methodInfo.env->SetLongArrayRegion(jLongArray, 0, elements, (jlong*) pattern);
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jLongArray, repeat);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
void cancelVibrateJNI()
{
JniMethodInfomethodInfo;
if (! getStaticMethodInfo(methodInfo, "cancelVibrate", "()V"))
{
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
3.2 修改CocosDenshion/include/SimpleAudioEngine.h 增加如下方法.
void vibrate(long long time);
void vibrateWithPattern(long long pattern[], int repeat);
void cancelVibrate();
3.3 修改CocosDenshion/android/SimpleAudioEngine.cpp 增加调用JNI的如下方法。
void SimpleAudioEngine::vibrate(long long time)
{
vibrateJNI(time);
}
void SimpleAudioEngine::vibrateWithPattern(long long pattern[], int repeat)
{
vibrateWithPatternJNI(pattern, repeat);
}
void SimpleAudioEngine::cancelVibrate()
{
cancelVibrateJNI();
}
4. 至此,代码上的修改都OK 啦,在游戏中需要使用震动的地方调用。
CocosDenshion::SimpleAudioEngine::sharedEngine()->vibrate(time);
CocosDenshion::SimpleAudioEngine::sharedEngine()->vibrateWithPattern(pattern,repeat);
取消震动用这个
CocosDenshion :: SimpleAudioEngine :: sharedEngine () -> cancelVibrate ();