Cocos2d-x through Jni realize Cocos2d-x interface jump to new Activity, jnicocos2d-x

Source: Internet
Author: User

Cocos2d-x through Jni realize Cocos2d-x interface jump to new Activity, jnicocos2d-x

Let's just talk about the source code.

1. Java Layer

1) Add the UserInfoActivity. java class in the org. cocos2dx. cpp directory. The Code is as follows:

Package org. cocos2dx. cpp; import com. packet.com. jni. r; // note the path import android. app. activity; import android. OS. bundle; import android. widget. textView; import android. content. intent; public class UserInfoActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. userinfo); Intent intent = getIntent (); String string = intent. getStringExtra ("name"); TextView textView = new TextView (this); textView. setTextSize (40); textView. setText (string); setContentView (textView );}}
2) AppActivity. java class

Package org. cocos2dx. cpp; import org. cocos2dx. lib. cocos2dxActivity; import org. cocos2dx. lib. cocos2dxGLSurfaceView; import android. content. intent; import android. OS. bundle; public class AppActivity extends Cocos2dxActivity {public static final int SHOW_DIALOG = 0x0001; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} // set OpenGL-related information public jsononcreateview () {javasglsurfaceview = new Cocos2dxGLSurfaceView (this); // GuideLayer shocould create stencel buffer surglfaceview. setEGLConfigChooser (5, 6, 5, 0, 16, 8); return glSurfaceView;} // load the library of the Cocos2d-x static {System. loadLibrary ("cocos2dcpp");} public static void Share () {// Java-layer nativa code of Jni, test. cpp implementation, and is called by C ++ Intent intent = new Intent (); intent. setClass (AppActivity. getContext (), UserInfoActivity. class); intent. putExtra ("name", "Hello World"); AppActivity. getContext (). startActivity (intent );}}

2. Add the. h and. cpp of the test class under the Jni directory of the jni layer.

Test. h

# Ifndef TEST_H _ # define TEST_H_extern "C" {// C ++ calls the Java function interface. This method is used in the menuCallback function of HelloWorldScene. Void Share () ;}# endif
Test. cpp

#include "test.h"#include "cocos2d.h"#include "platform/android/jni/JniHelper.h"#include "../../../Classes/JniTest.h"#include <jni.h>//#define CLASS_NAME "org/cocos2dx/cpp/JniTestHelper"#define CLASS_NAMENEW "org/cocos2dx/cpp/AppActivity"using namespace cocos2d;extern "C"{void Share(){bool hasMethod;JniMethodInfo t;hasMethod = JniHelper::getStaticMethodInfo(t,CLASS_NAMENEW,"Share","()V");if(hasMethod){log("Share function");if(t.methodID){t.env->CallStaticVoidMethod(t.classID,t.methodID);log("function share() was called");}else{log("function share was not called");}}else{log("function share was not found");}}}

3. C ++ Layer
void HelloWorld::menuCloseCallback(Ref* pSender){#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)    Share();    #endif    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}
4. Note: add the header file for determining the platform.
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "../proj.android/jni/hellocpp/test.h"#endif
5. Running result




Cocos2d-x scenario switching problem?

First Program Finance welcome page
CCDirector: sharedDirector ()-> runWithScene (Welcome to scene );
When switching a program, use the following code:
CCDirector: sharedDirector ()-> replaceScene (game interface scene );
This can be achieved.

In the cocos2d-x, I want to bring up a dialog in the activity side, use PopupWindow, but there is a problem such as Supplement

I don't know what your problem is.
Your window is implemented in java, and The Calling Interface is exposed to cocos2d. JNI code can show window, and of course the close method can be exposed to destroy the window.

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.