Recently just to add mobile phone vibration function in the hand tour. No, ah, the Internet to find the next, the results of related articles or a lot of, speak also quite detailed, but I find all to change cocos2dx of the bottom code. I looked at it, totally unnecessary. It's OK to define a static method in your own Java file.
Java needs to add content:
Find your own Java class (the main entry class is also OK), define the following static method in the class:
public static void vibrate (int vibratetime) {
Vibrator Vib =
(vibrator) Mainclass.returncontent (). Getsystemservice (Service.vibrator_service);
Vib.vibrate (Vibratetime);
}
MainClass: is the main entrance of your game Java file name
You need to define and add the following in the main portal Java class:
private static Activity mactivity;
Mainclass.mactivity = this;
public static Activity Returncontent ()
{
return mactivity;
}
What C + + needs to add:
Find a C + + class and add this method
parameter is the number of milliseconds
Vibratorjni (int vibratortime)
{
Jnimethodinfo T;
const char * Jniclass = "Fills this string with the Java class name of the vibrate method";
if (Jnihelper::getstaticmethodinfo (t, Jniclass, "vibrate", "(I) V"))
{
T.env->callstaticvoidmethod (T.classid, T.methodid, vibratortime);
T.env->deletelocalref (T.CLASSID);
}
}
Call Method:
1. Calling in C + +
C++classname::vibratorjni (500);
C++classname: Is the class name where the Vibratorjni method resides;
2. Calling in Lua
First inject the C + + method into the LUA stack
Vibrating phone
int Lua_vibratorjni (lua_state * L)
{
int time;
Time = (int) lua_tonumber (L, 1);
Jniutil::vibratorjni (time);
return 1;
}
void Lua_setupapi (lua_state * L)
{
Lual_reg lib[] =
{
{"Shakephone", lua_vibratorjni},//vibrating phone
{null, NULL},
};
Lual_register (L, "Ctolua", Lib);
}
Lua_setupapi This function is called before entering the main LUA portal. Typically called in AppDelegate.cpp
Called directly in LUA code
Ctolua.shakephone (500);
Mobile games open Android phone shake