cocos2d-x3.2中添加Android手机震动

前端之家收集整理的这篇文章主要介绍了cocos2d-x3.2中添加Android手机震动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本人宣布从此博文发出后,我的cocos2dx的引擎从cocos2dx3.1.1跳到cocos2dx3.2,哈哈,其实变化不大的,不碍事~~~

下面来说说在cocos中添加Android手机震动的功能,亲身体验,网上的教程都是渣渣啊,坑比的很,还要看了原帖http://www.cocos2d-x.org/boards/6/topics/8179全英文的,蛋疼恼火了许久,才解决~~

下面进入正题。相信看完本文,什么问题都ok了!!!!

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) @H_403_74@mContext.getSystemService(Context.@H_403_74@VIBRATOR_SERVICE);

v.vibrate(time);

}

/** * @param pattern震动时间数组 EG:{500,200,500,300}*@param repeat重复次数*/

public void vibrateWithPattern(long[] pattern,int repeat) {

Vibratorv = (Vibrator) @H_403_74@mContext.getSystemService(Context.@H_403_74@VIBRATOR_SERVICE);

v.vibrate(pattern,repeat);

}

/*** 取消震动 */

public void cancelVibrate() {

Vibratorv = (Vibrator) @H_403_74@mContext.getSystemService(Context.@H_403_74@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,elements,(jlong*) pattern);

methodInfo.env->CallStaticVoidMethod(methodInfo.classID,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();

最后在加上这个

Coco2dxHelper.java中

public static void vibrate(long time)
{
sCocos2dSound.vibrate(time);
}

public static void vibrateWithPattern(long[] pattern,int repeat) {
sCocos2dSound.vibrateWithPattern(pattern,repeat);
}
/**
* 取消震动


到这里就圆满完成了,网上的教程没有最后一步,所以说最后一步尤其关键。

本人cocos2dx 2.x和3.x的源码淘宝地址(欢迎大家光顾):https://shop141567464.taobao.com/?spm=a313o.7775905.1998679131.d0011.pzUIU4


@H_751_403@不懂的可以加我的QQ群: 239982941(cocos2d-x 3.x学习群)欢迎你的到来哦,看了博文给点脚印呗,谢谢啦~~

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