<cocos2dx> 关于cocos2dx接入sdk的一些坑

   简单说说,我相信每个人做Android的都知道的更新UI只能在UI线程进行刷新,这是一个非常霸道的条款

   而且,我们的UI线程不能被阻塞。


   在接入支付SDK的过程,任何的支付SDK都会自带有支付的界面,这个时候就涉及到我们的UI刷新了,如果你不小心直接通过JNI调的函数来

   调用支付界面的话,轻则无响应,重则直接挂了,hhh~~


   1:cocos2dx是如何实现跨平台的

Cocos2dx基于opengl es 实现跨平台工程

Android程序在启动的时候,首先加载了我们的MainActivity,这个时候Java加载了c++平台编译的.so文件。然后我们在根据Activity的声明周期走起来~

public void init() {
		
    	// FrameLayout
        ViewGroup.LayoutParams framelayout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.FILL_PARENT);
        FrameLayout framelayout = new FrameLayout(this);
        framelayout.setLayoutParams(framelayout_params);

        // Cocos2dxEditText layout
        ViewGroup.LayoutParams edittext_layout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.WRAP_CONTENT);
        Cocos2dxEditText edittext = new Cocos2dxEditText(this);
        edittext.setLayoutParams(edittext_layout_params);
        // ...add to FrameLayout
        framelayout.addView(edittext);

        // Cocos2dxGLSurfaceView
        this.mGLSurfaceView = this.onCreateView();

        // ...add to FrameLayout
        framelayout.addView(this.mGLSurfaceView);

        // Switch to supported OpenGL (ARGB888) mode on emulator
        if (isAndroidEmulator())
           this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);

        this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
        this.mGLSurfaceView.setCocos2dxEditText(edittext);

        // Set framelayout as the content view
		setContentView(framelayout);
	}

主要基于GLSurfaceView,将GLSurfaceView添加到我们的视图中。设置了renderer去调起了GLSurfaceView的onSurfaceCreated()

扫描二维码关注公众号,回复: 2165918 查看本文章
public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig) {
		Cocos2dxRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight);
		this.mLastTickInNanoSeconds = System.nanoTime();
	}

这行代码就很清晰了,通过native方法去调用我们的cocos2dx的初始化入口函数。

2:如何去刷新UI

这个时候我们应该知道了,我们的cocos2dx和Android的UI其实是运行在俩个线程的,分别是UI线程和GLSurfaceView的线程

如果我们需要刷新UI就得主要分辨清楚了。

3:使用示例

cocos2dx---->Android

bool MainLayer::init(){

	CCLayer::init();

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)//通知Android显示dialog
		CallJava::onShowDialog();
	#endif
}
这里是cocos2dx的C++代码,在初始化页面的时候调用Android的dialog框

public static void onShowDialog(){
    	runrun.getInstance().runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				AlertDialog dialog = new AlertDialog.Builder(runrun.getInstance()).create();
				dialog.setTitle("cocos2dx:");
				dialog.setMessage("Android");
				dialog.setCancelable(false);
		    	dialog.show();
			}
		});
    }
这里,我们需要先切换到UI线程来,再show出我们的dialog


Android---->Cocos2dx

activity.runOnGLThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Log.e("--->", "Untils_  "+Thread.currentThread().getName());
					CallCpp.getItem(id);
				}
			});

这里切换到GLSurfaceView的线程,刷新了UI。



猜你喜欢

转载自blog.csdn.net/guim_007/article/details/77319153