cocos2dx震动实现

前端之家收集整理的这篇文章主要介绍了cocos2dx震动实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

根据网上总结,自己亲自实现了的,90%靠谱率

一:c++端

1,修改CocosDenshion\android\jni目录下的两个文件SimpleAudioEngineJni.cpp,SimpleAudioEngineJni.h

SimpleAudioEngineJni.h

增加内容

<span style="white-space:pre">	</span>extern void vibrateJNI(long long time);
	extern void vibrateWithPatternJNI(long long pattern[],int repeat);
	extern void cancelVibrateJNI();
SimpleAudioEngineJni.cpp

增加内容(实现)

<span style="white-space:pre">	</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 style="white-space:pre">	</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 ) ;

完成!

猜你在找的Cocos2d-x相关文章