Cocos2d-x transplanted Android added vibration effect

Source: Internet
Author: User

CPP section calls Java static functions via JNI

Header file:

#include <jni.h> #include "cocos2d.h" #include "platform/android/jni/jnihelper.h"


Define methods in the CPP class to invoke the vibrate method in Cocos2dxsound.java:

static void Vibratejni (long longtime) {Jnimethodinfo methodinfo;if (! Jnihelper::getstaticmethodinfo (MethodInfo, "Org/cocos2dx/lib/cocos2dxsound", "Vibrate", "(J) V")) {Cclog ("Error: Vibrate is not there! "); return;} Methodinfo.env->callstaticvoidmethod (methodinfo.classid,methodinfo.methodid,time);methodInfo.env-> Deletelocalref (METHODINFO.CLASSID);}


In Android engineering Src/org/cocos2dx/lib/cocos2dxsound.java define the vibrate method:

private static Context mContext2;


In the Cocos2dxsound () method, add

This.mcontext2 = PContext;


Defining the Vibrate method

public static void vibrate (long time) {Vibrator V = (vibrator) mcontext2.getsystemservice (Context.vibrator_service); V.vibrate (time);}


In the CPP code, you need to call the following

Jniutil::vibratejni (500);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.