About the mutual invocation between Cocos2d-x and Android

Source: Internet
Author: User

Recently in the study of Cocos2d games transplant Android needs to call many methods, so in the study write them to call each other



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 calling wirelessly;

Testfunwithstringandrtn The method to be called in the class

(I) V return value and type of incoming parameter


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);
}

By completing the above steps, you can test the code for cocos2d calling wirelessly.

Then Android calls Cocos2d

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

in which to join

#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 ();
}


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


Cclog ("Test whether to enter");

}
}

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 that you want to invoke in the Java class that you want to call cocos2d as follows

Public native static void Callbackmobiles (); Names are defined, but native cannot be lost.

Then call Callbackmobiles () directly in the class

You can go directly to the Cclog ("Test for entry");


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

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

If it feels good for you, give me a review.









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.