疏忽導致易接SDK OpenGL error


猶豫把 SFLuaAdapter 初始化OnGLThread 在GL線程中回調寫成 OnUiThread了。。導致了OpenGL error 以爲是易接沒有在GLThread中回調。然後自己實現了一遍同樣的接口。後來越想越不對翻看易接文檔多次才發現他有註冊 GLThread 回調。主要是以前纔開始接入什麼都拷貝,然後自己修改init回調。然後沒在意,強迫症犯了才修改。

易接代碼:

		SFLuaAdapter.init(Cocos2dxHelper.getActivity(), new SFActionCallback() {

			@Override
			public void callback(Runnable run) {
				AppActivity.this.runOnGLThread(run);
			}
		});

錯誤修改

		SFLuaAdapter.init(Cocos2dxHelper.getActivity(), new SFActionCallback() {

			@Override
			public void callback(Runnable run) {
				Cocos2dxHelper.runOnUiThread(run);
			}
		});
由於程序員的強迫症犯了,我想要的修改:

		/* 易接SDK Begin */
		SFLuaAdapter.init(Cocos2dxHelper.getActivity(), new SFActionCallback() {

			@Override
			public void callback(Runnable run) {
				Cocos2dxHelper.runOnGLThread(run);
			}
		});
		/* 易接SDK End */

重新實現一遍的接口,



package org.cocos2dx.lua;

import java.util.HashMap;
import java.util.Map;

import org.cocos2dx.lib.Cocos2dxHelper;
import org.cocos2dx.lib.Cocos2dxLuaJavaBridge;
import org.json.JSONException;
import org.json.JSONObject;

import com.snowfish.cn.ganga.offline.helper.SFCommonSDKInterface;
import com.snowfish.cn.ganga.offline.helper.SFExtendListener;
import com.snowfish.cn.ganga.offline.helper.SFGameExitListener;
import com.snowfish.cn.ganga.offline.helper.SFIPayResultListener;
import com.snowfish.cn.ganga.offline.helper.SFOfflineInitListener;

public class YiJieLua {
	private static boolean isInit = false;
	public static void onPause(){
		if(isInit)
			SFCommonSDKInterface.onPause(Cocos2dxHelper.getActivity());
	}
	
	public static void onDestroy(){
		if(isInit)
			SFCommonSDKInterface.onDestroy(Cocos2dxHelper.getActivity());
	}
	
	public static void onResume(){
		if(isInit)
			SFCommonSDKInterface.onResume(Cocos2dxHelper.getActivity());
	}
	
	public static void onInit(final int luaFunc) {
		if (isInit){
			return;
		}
		isInit = true;
		Cocos2dxHelper.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				SFCommonSDKInterface.onInit(Cocos2dxHelper.getActivity(), new SFOfflineInitListener() {

					@Override
					public void onResponse(String tag, String value) {
						// TODO Auto-generated method stub
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("tag", tag);
							jsonObject.put("value", value);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), false);
					}
				});
			}
		});
	}
	
	public static void viewMoreGames(){
		Cocos2dxHelper.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				SFCommonSDKInterface.viewMoreGames(Cocos2dxHelper.getActivity());	
			}
		});
	}
	
//	public static void extend(final String data, final Map<String, Integer> luaFuncs){
//		Cocos2dxHelper.runOnUiThread(new Runnable() {
//			
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				Map<String, SFExtendListener> sfExtendMap = new HashMap<String, SFExtendListener>();
//				for (final Map.Entry<String, Integer> entry : luaFuncs.entrySet()) {
//					sfExtendMap.put(entry.getKey(), new SFExtendListener() {
//						
//						@Override
//						public void onResponse(String tag, String value) {
//							// TODO Auto-generated method stub
//							
//							JSONObject jsonObject = new JSONObject();
//							try {
//								jsonObject.put(tag, value);
//							} catch (JSONException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							}
//							Cocos2dxHelper.runOnGLThread(entry.getValue(), jsonObject.toString(), true);
//						}
//					});
//				}
//				SFCommonSDKInterface.extend(Cocos2dxHelper.getActivity(), data, sfExtendMap);
//			}
//		});
//	}
	
	public static void extend(final String data, final int count, final int luaFunc){		
		Cocos2dxHelper.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String, SFExtendListener> sfExtendMap = new HashMap<String, SFExtendListener>();
				for (int i = 0; i < count; i++) {
					final int luaFuncIndex = i;
					Cocos2dxLuaJavaBridge.retainLuaFunction(luaFunc);
					sfExtendMap.put(String.valueOf(i), new SFExtendListener() {
						
						@Override
						public void onResponse(String tag, String value) {
							// TODO Auto-generated method stub
							
							JSONObject jsonObject = new JSONObject();
							try {
								jsonObject.put("result", luaFuncIndex);
								jsonObject.put("value", value);
								jsonObject.put("tag", tag);
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
						}
					});
				}
				Cocos2dxLuaJavaBridge.releaseLuaFunction(luaFunc);
				SFCommonSDKInterface.extend(Cocos2dxHelper.getActivity(), data, sfExtendMap);
			}
		});
	}
	
	public static String getUserId(){
		return String.valueOf(SFCommonSDKInterface.getUserId());
	}

	public static boolean isPaid(String payId) {
		return SFCommonSDKInterface.isPaid(Cocos2dxHelper.getActivity(), payId);
	}

	public static void setPaid(String payId) {
		SFCommonSDKInterface.setPaid(Cocos2dxHelper.getActivity(), payId);
	}

	public static boolean isMusicEnable(){
		return SFCommonSDKInterface.isMusicEnabled(Cocos2dxHelper.getActivity());
	}
	
	public static void onExit(final int luaFunc){
		Cocos2dxHelper.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				SFCommonSDKInterface.onExit(Cocos2dxHelper.getActivity(), new SFGameExitListener() {
					
					@Override
					public void onGameExit(boolean isExit) {
						// TODO Auto-generated method stub
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", isExit ? "exit" : "noExit");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}
				});
			}
		});
	}
	
	public static void recharge(final int price, final String chargeDesc, final String sign, final int luaFunc){
		Cocos2dxHelper.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				SFCommonSDKInterface.recharge(Cocos2dxHelper.getActivity(), price, chargeDesc, sign, new SFIPayResultListener() {

					@Override
					public void onSuccess(String arg0) {
						// TODO Auto-generated method stub
						System.out.println("onSuccess = " + arg0);
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", "success");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}

					@Override
					public void onFailed(String arg0) {
						// TODO Auto-generated method stub
						System.out.println("onFailed = " + arg0);
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", "fail");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}

					@Override
					public void onCanceled(String arg0) {
						// TODO Auto-generated method stub
						System.out.println("onCanceled = " + arg0);
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", "cancel");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}
				});
				
			}
		});
	}
	
	public static void pay(final String payid, final int luaFunc) {
		Cocos2dxHelper.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				SFCommonSDKInterface.pay(Cocos2dxHelper.getActivity(), payid, new SFIPayResultListener() {

					@Override
					public void onSuccess(String arg0) {
						// TODO Auto-generated method stub
						System.out.println("onSuccess = " + arg0);
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", "success");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}

					@Override
					public void onFailed(String arg0) {
						// TODO Auto-generated method stub
						System.out.println("onFailed = " + arg0);
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", "fail");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}

					@Override
					public void onCanceled(String arg0) {
						// TODO Auto-generated method stub
						System.out.println("onCanceled = " + arg0);
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("result", "cancel");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Cocos2dxHelper.runOnGLThread(luaFunc, jsonObject.toString(), true);
					}
				});
			}
		});

	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章