Add Android phone shake in cocos2d-x3.2

Source: Internet
Author: User

I announced from this blog post, my cocos2dx engine from cocos2dx3.1.1 jump to cocos2dx3.2, haha, in fact, the change is small, not in the way ~ ~ ~

The following is said in the Cocos add Android phone vibration features, personal experience, online tutorials are slag ah, pit ratio is very, but also to see the original post http://www.cocos2d-x.org/boards/6/topics/8179, all in English, Egg pain annoyed for a long time, just solve ~ ~

Let's get down to the chase. I believe that after reading this article, what problems are OK!!!

1, in proj.android this directory


Androidmanifest.xml File to add vibration permissions to the app

<uses-permission android:name="Android.permission.VIBRATE" />


2, Modify the Android project source file Src/org/cocos2dx/lib/cocos2dxsound.java increase the vibration method, call Android Bottom Vibrator

/*** @param Time Vibration Time */

Public void vibrate (long time) {

Vibratorv = (vibrator) mcontext. Getsystemservice (Context. Vibrator_service);

V.vibrate (time);

}

/** * @param pattern Vibration Time Array eg:{500,200,500,300}* @param Repeat Number of repetitions */

Public void Vibratewithpattern (long[] pattern,int repeat) {

Vibratorv = (vibrator) mcontext. Getsystemservice (Context. Vibrator_service);

V.vibrate (pattern,repeat);

}

/*** Remove Vibrations */

Public void cancelvibrate () {

Vibratorv = (vibrator) mcontext. Getsystemservice (Context. Vibrator_service);

V.cancel ();

}

3. at this point, the changes in the Java Project OK, the following is the addition of the Jni method in the Cocos2d-x C + + to remove the use of Java Vibration method is ok!

3.1. Modifying the libcocosdenshion project cocos2d-x\cocosdenshion\android\ SimpleAudioEngineJni.h and SimpleAudioEngineJni.cpp methods under the Jni folder

Add in 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__

Add in 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, 0, elements, (jlong*) pattern);

Methodinfo.env->callstaticvoidmethod (Methodinfo.classid, Methodinfo.methodid, 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 Modify Cocosdenshion/include/simpleaudioengine.h to add the following method.

void vibrate (long long time);

void Vibratewithpattern (Long long pattern[], int repeat);

void Cancelvibrate ();

3.3 Modify Cocosdenshion/android/simpleaudioengine.cpp to add the following method for calling 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. at this point, the code changes are OK , in the game need to use the vibration of the place to call.

Cocosdenshion::simpleaudioengine::sharedengine ()->vibrate (time);

Cocosdenshion::simpleaudioengine::sharedengine ()->vibratewithpattern (pattern,repeat);

Remove the vibrations with this

cocosdenshion :: Simpleaudioengine :: Sharedengine ()cancelvibrate();

finally in Add this.



There is no final step in the online tutorial, so the final step is especially critical.


Do not know can add my QQ group: 239982941 (Cocos2d-x 3.x Learning Group) Welcome your arrival Oh, read the blog to point Footprints Bai, thank you ~ ~


Add Android phone shake in cocos2d-x3.2

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.