About the mutual invocation between Cocos2d-x and Android

Source: Internet
Author: User

There are a number of methods that need to be called in recent studies of cocos2d game transplant Android. So in the study, write down the calls between them.



First, Cocos2d calls Android

Add a header file to an. h file

#include <jni.h>
#include "Platform\android\jni\jnihelper.h"


Jnimethodinfo minfo;//Defining a JNI function information structure body
The Getstaticmethodinfo function returns a bool value indicating whether this function is found
BOOL Ishave = Jnihelper::getstaticmethodinfo (Minfo, "Com/zhangying/jni/applay", "Testfunwithstringandrtn", "(I) V");

The com/zhangying/jni/applay parameter is the full path of the class that called wirelessly.

Testfunwithstringandrtn The method to be called in the class

(I) V The return value and the type of the pass-in number


Jobject jobj;
if (!ishave) {
Cclog ("does not exist");
}
else{
Cclog ("presence");
Call this function
Jobj = Minfo.env->callstaticobjectmethod (Minfo.classid, Minfo.methodid, This->choicemoney);


readdata::setuserdefaultint ("Recharge", This->choicemoney);
}

After the above steps can test cocos2d call wirelessly code

Then Android calls Cocos2d

First find the Hellocpp in the JNI in the project main.cpp

Increase in

#include <android/log.h>
#include "Data/readdata.h"


#define LOG_TAG "Main"
#define LOGD (...) __android_log_print (android_log_debug,log_tag,__va_args__)


using namespace cocos2d;
void Cocos_android_app_init (jnienv* env, Jobject thiz) {
LOGD ("Cocos_android_app_init");
Appdelegate *pappdelegate = new Appdelegate ();
}


Some of the code is self-added above some of the own without changes
extern "C"
{
#if (Cc_target_platform = = cc_platform_android)
void Java_com_yiqu_mobilesdk_iap_iaporder_callbackmobiles (jnienv *env, Jobject thiz)
{


Cclog ("Test whether enter");

}

#endif
}

Attention:

The name Java_com_yiqu_mobilesdk_iap_iaporder_callbackmobiles is not a random definition.

Java is fixed COM.YIQU.MOBILESDK.IAP is the package name Iaporder is the class name, Callbackmobiles is the method name you want to call

That's how it's written.




Write the method name you want to invoke in the Java class that you want to call cocos2d, such as the following

Public native static void Callbackmobiles (); The name is defined casually, but native can't throw away

Then call Callbackmobiles () directly in the class

will be able to enter directly into the Cclog ("test whether enter");


Haha ended, this is I am using the game to pay a piece of code call, the execution is normal, if there is bad place to look point

Can also enter the group:252799892 We communicate with each other

Let's say the feeling is helpful to you.









About the mutual invocation between Cocos2d-x and Android

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.