Cocos2DX C ++ adjusts Java non-static functions.

Calling java non-static functions in Cocos2dx C++.

Calling the non-static method in JAVA in CPP requires obtaining the object of Activity first. This is the difference.

Declare a static variable in AppActivity
public span> static AppActivity con;
to this variable in the create of AppActivity Assignment
@Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); con = this; }
Add a method to obtain the object in AppActivity
public static Object returnActivity() {return con; }
Add method in cpp
void HelloWorld::onClickLogin(){#if(CC_TARGET_PLATFORM < span class="hljs-subst">== CC_PLATFORM_ANDROID) {JniMethodInfo info; jobject activityObj; bool isHaveObj = JniHelper::getStaticMethodInfo(info, "org/cocos2dx/cpp/AppActivity", "returnActivity"< /span>, "()Ljava/lang/Object;"); if (isHaveObj) {activityObj = info.env->CallStaticObjectMethod( info.classID, info.methodID); log span>("jni:obj has find.");} else {log("jni:obj is null");} JniMethodInfo methodInfo; bool isHaveMethod = JniHelper::getMethodInfo(methodInfo, "org/cocos2dx/cpp/AppActivity", "onClickLogin", "() V"); if (isHaveMethod) {methodInfo.env->CallVoidMethod(activityObj, methodInfo.methodID);} }#endif}

Leave a Comment

Your email address will not be published.