根据网上总结,自己亲自实现了的,90%靠谱率
一:c++端
1,修改CocosDenshion\androID\jni目录下的两个文件SimpleAudioEngineJni.cpp,SimpleAudioEngineJni.h
SimpleAudioEngineJni.h
增加内容
<span > </span>extern voID vibrateJNI(long long time); extern voID vibrateWithPatternJNI(long long pattern[],int repeat); extern voID cancelVibrateJNI();SimpleAudioEngineJni.cpp
增加内容(实现)
<span > </span>voID vibrateJNI(long long time) { JniMethodInfo methodInfo; 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) { JniMethodInfo methodInfo; if (! getStaticmethodInfo(methodInfo,"vibrateWithPattern","([JI)V")) { return; } int elements = sizeof(pattern); jlongArray jLongArray = methodInfo.env->NewLongArray(elements); methodInfo.env->SetLongArrayRegion(jLongArray,elements,(jlong*) pattern); methodInfo.env->CallStaticVoIDMethod(methodInfo.classID,jLongArray,repeat); methodInfo.env->DeleteLocalRef(methodInfo.classID); } voID cancelVibrateJNI() { JniMethodInfo methodInfo; if (! getStaticmethodInfo(methodInfo,"cancelVibrate","()V")) { return; } methodInfo.env->CallStaticVoIDMethod(methodInfo.classID,methodInfo.methodID); methodInfo.env->DeleteLocalRef(methodInfo.classID); }2,在\CocosDenshion\include下SimpleAudioEngine.h增加如下内容(就是声明,你懂的)
<span > </span>voID vibrate(long long time); voID vibrateWithPattern(long long pattern[],int repeat); voID cancelVibrate();3 到这里其实有效的部分已经完成了,但是还要增加win32相应地方,因为我们的代码毕竟是在windows平台上编译,所以调用还是会调用CocosDenshion\win32下的方法,所以还要在目录CocosDenshion\win32下增加如下代码
voID SimpleAudioEngine::vibrate(long long time){}voID SimpleAudioEngine::vibrateWithPattern(long long pattern[],int repeat){}voID SimpleAudioEngine::cancelVibrate(){}
空函数就可以了,不需要实现,这里只是为了之后可以编译链接(其实是链接)的时候可以通过,找到函数而已。 当然也可以不写这些东西,那么在调用震动方法的代码地方加上平台判断也可以#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 也可以。
二、 androID端
AndroIDManifest.xml中加震动权限
<uses-permission androID:name="androID.permission.VIBRATE" />
编辑org.cocos2dx.lib下的Cocos2dxSound.java文件,增加如下内容
/*** @param time 震动时间*/ public voID vibrate(long time) { Vibrator v = (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) { Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(pattern,repeat); } /*** 取消震动 */ public voID cancelVibrate() { Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); v.cancel(); }在文件Cocos2dxHelper.java中增加如下内容
//vibrate 相关 public static voID vibrate(long time){ Cocos2dxSound cocos2dxsound = new Cocos2dxSound(sContext); cocos2dxsound.vibrate(time); } public static voID vibrateWithPattern(long[] pattern,int repeat){ Cocos2dxSound cocos2dxsound = new Cocos2dxSound(sContext); cocos2dxsound.vibrateWithPattern(pattern,repeat); } public static voID cancelVibrate() { Cocos2dxSound cocos2dxsound = new Cocos2dxSound(sContext); cocos2dxsound.cancelVibrate(); }OK了,准备代码完成了
调用,就用下面的了
CocosDenshion :: SimpleAudioEngine :: sharedEngine ( ) -> vibrate ( time ) ; vibrateWithPattern pattern , repeat ; CocosDenshion :: SimpleAudioEngine :: sharedEngine ( ) -> cancelVibrate ) ;完成!
总结以上是内存溢出为你收集整理的cocos2dx震动实现全部内容,希望文章能够帮你解决cocos2dx震动实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)