Cocos2d-x3.2 to add Android phone shake, cocos2dandroid

Source: Internet
Author: User

Cocos2d-x3.2 to add Android phone shake, cocos2dandroid

I announced that after this blog post, my cocos2dx engine jumped from cocos2dx3.1.1 to cocos2dx3.2 ~~~

The following describes how to add the Android mobile phone vibration function to cocos for a personal experience. The online tutorials are full of scum ~~

Next, go to the topic. After reading this article, I believe everything is okay !!!!

1. Under the proj. android directory


AndroidManifest. xml file, giving the app the vibration permission

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


2. Modify the source file src/org/cocos2dx/lib/Cocos2dxSound. java to add the vibration method and call the underlying android vibrator.

/***@ ParamTime vibration time */

Public voidVibrate (LongTime ){

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

V. vibrate (time );

}

/***@ ParamPattern vibration time array EG: {500,200,500,300 }*@ ParamRepeat times */

Public voidVibrateWithPattern (Long[] Pattern,IntRepeat ){

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

V. vibrate (pattern, repeat );

}

/*** Cancel the vibration */

Public voidCancelVibrate (){

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

V. cancel ();

}

3. So far, the modification in the java project is OK, the following is to add the JNI method in the Cocos2d-x so that C ++ to remove the vibration method with Java OK!

3. 1. Modify SimpleAudioEngineJni. h and SimpleAudioEngineJni. cpp methods under the cocos2d-x \ cococosdenshion \ android \ jni folder in the libCocosDenshion Project

Add

# Ifndef _ SIMPLE_AUDIO_ENGINE_JNI __

# Define _ SIMPLE_AUDIO_ENGINE_JNI __

# Include

Extern "C"

{

......

// Add thesementhod

Extern void vibrateJNI (long time );

Extern void vibrateWithPatternJNI (long pattern [], intrepeat );

Extern void cancelVibrateJNI ();

}

# Endif/_ SIMPLE_AUDIO_ENGINE_JNI __

 

 

 

Add

 

// Add these jni menthod

Void vibrateJNI (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 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 Add the following method to modify CocosDenshion/include/SimpleAudioEngine. h.

Void vibrate (long time );

Void vibrateWithPattern (long pattern [], int repeat );

Void cancelVibrate ();

3.3 modify CocosDenshion/android/SimpleAudioEngine. cpp and add the following method to call JNI.

Void SimpleAudioEngine: vibrate (long time)

{

VibrateJNI (time );

}

Void SimpleAudioEngine: vibrateWithPattern (long pattern [], int repeat)

{

VibrateWithPatternJNI (pattern, repeat );

}

Void SimpleAudioEngine: cancelVibrate ()

{

CancelVibrateJNI ();

}

4. Now, all the code modifications are OK. You need to call them in the place where the game is shaken.

CocosDenshion: SimpleAudioEngine: sharedEngine ()-> vibrate (time );

CocosDenshion: SimpleAudioEngine: sharedEngine ()-> vibrateWithPattern (pattern, repeat );

Use this to cancel the vibration

CocosDenshion: SimpleAudioEngine: sharedEngine ()-> cancelVibrate ();

Finally, add this



This is a successful conclusion. There is no final step in the online tutorial, so the final step is particularly critical.


Don't understand can add my QQ group: 239982941 (cocos2d-x 3. x Learning Group) Welcome to your arrival Oh, read the blog to some footprints Bai, thank you ~~



How does android shake mobile phones?

Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE); long [] pattern = {800, 50,400, 30}; // OFF/ON/OFF/ON... vibrator. vibrate (pattern, 2); //-1 is not repeated, and non-1 is repeated starting from the specified subscript of pattern
 
How does Android 21 disable text message vibration?

I don't know if you are using your own text message software.
If yes, enter the text message, press the menu key, select Settings, check "notification", and then select the "Notification Settings" below. The vibration mode is selected, select "Never vibrate.

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.