cocos2d-x 3.2 android手机震动

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

1.编写java端


打开Cocos2dxHelper.java




编写函数


/*time震动时间*/
public static void vibrate(int vibrateTime) {
Vibrator vib = (Vibrator) sActivity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(vibrateTime);
}

/*取消震动*/
public void cancelVibrate(){
Vibrator vib = (Vibrator)sActivity.getSystemService(Service.VIBRATOR_SERVICE);
vib.cancel();
}




2.编写cocos2d-x端


.h文件

static void vibrate(int time);

static void cancelVibrate();



.cpp

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include <android/log.h>
#include "platform/android/jni/JniHelper.h"
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"

#endif


void HelloWorld::vibrate(int time)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t,CLASS_NAME,"vibrate","(I)V"))
{
t.env->CallStaticVoidMethod(t.classID,t.methodID,time);
t.env->DeleteLocalRef(t.classID);
}
#endif
}


void HelloWorld::cancelVibrate()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t,"cancelVibrate","()V"))
{
t.env->CallStaticVoidMethod(t.classID,t.methodID);
t.env->DeleteLocalRef(t.classID);
}
#endif
}


3.使用方法


vibrate(5000);

原文链接:https://www.f2er.com/cocos2dx/341913.html

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