码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx震动实现

时间:2015-04-08 15:12:13      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   震动   

根据网上总结,自己亲自实现了的,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, 0, elements, (jlong*) pattern);
		methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, 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);
CocosDenshion::SimpleAudioEngine::sharedEngine()->vibrateWithPattern(pattern, repeat);
CocosDenshion::SimpleAudioEngine::sharedEngine()->cancelVibrate();


完成!

cocos2dx震动实现

标签:cocos2d-x   震动   

原文地址:http://blog.csdn.net/mincheat/article/details/44938385

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!