码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x通过Jni实现Cocos2d-x界面跳转到新的Activity

时间:2014-11-03 01:24:16      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:activity   android   cocos2d-x3.3rc0   

废话不多说,直接上源码

1、Java层

1)首先在org.cocos2dx.cpp目录下添加新类UserInfoActivity.java,该类自行定义即可,代码如下:

package org.cocos2dx.cpp;

import com.pactera.jni.R;//注意路径
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类

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);
	}
	//设置OpenGL的相关信息
	public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // GuideLayer should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
         
        return glSurfaceView;
    }
	//加载Cocos2d-x的库
	static {
		
		System.loadLibrary("cocos2dcpp");
	}
	public static void Share(){   //Jni的Java层nativa代码,在Jni目录下的test.cpp实现,并被C++调用
		 Intent intent = new Intent();  
         intent.setClass(AppActivity.getContext(), UserInfoActivity.class);  
         intent.putExtra("name", "Hello World");  
         AppActivity.getContext().startActivity(intent);  
    }
}

2、Jni层

jni目录下添加test类的.h和.cpp

test.h

#ifndef TEST_H_
#define TEST_H_
extern "C"
{
	//C++调Java的函数接口,该方法在HelloWorldScene中menuCallback函数中使用。
	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++层

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、注意事项

添加平台判断头文件
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "../proj.android/jni/hellocpp/test.h"
#endif

5、运行结果

bubuko.com,布布扣bubuko.com,布布扣



Cocos2d-x通过Jni实现Cocos2d-x界面跳转到新的Activity

标签:activity   android   cocos2d-x3.3rc0   

原文地址:http://blog.csdn.net/yuxikuo_1/article/details/40721901

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!