Mobile games open Android phone shake

Source: Internet
Author: User
Tags lua

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

Related Article

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.