[Creator 2.1.x] Vibrate

Hi All,

How do we vibrate device in Cocos Creator?

Thanks in advance!

Perhaps these can point you in the right direction

https://web.archive.org/web/*/http://www.cocos2d-x.org/wiki/Invoking_Objective-C_methods_from_JavaScript

Thanks for your reply.

I saw it but, its for calling static methods?

So, we have to write vibration codes in native and expose it as a static method to be called by JS?

How could we miss such an important feature for games in a popular game engine! Or Did i miss anything?

I think this is a critical problem for such an engine like Creator.
No example code given although too many questions raised.
Waiting for the official answer from admins.

@jare any help/advise?

I don’t know if the cocos accept java. i think yes. I’m new on cocos, but if you want vibrate, you need to use tha code on your font. you call some java library. in your source …

import android.os.Vibrator;
import android.os.Vibrator;
...
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
    //deprecated in API 26 
// If you want to vibrate indefinely put 0.
    v.vibrate(500);
}

//If you want a diffrent vibration you can use If with a VibrationEffect (createOneShot){}
//(Acc therms)

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