Skip to content

Instantly share code, notes, and snippets.

@goodzsq
Last active August 29, 2015 14:05
Show Gist options
  • Save goodzsq/08f0ff1e8dd27b81d760 to your computer and use it in GitHub Desktop.
Save goodzsq/08f0ff1e8dd27b81d760 to your computer and use it in GitHub Desktop.
cocos2dx AppActivity 常用操作
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class AppActivity extends Cocos2dxActivity {
private static AppActivity appActivity = null;
private static int diamondPackType = 0;
//调用C++里的全局函数
public static native void callCpp(int pType);
public static String test(){
//被C++调用时注意一个坑 若返回空字符串 则c++得到的结果是随机的
return "";
}
//被C++调用时候最好以发送消息的形式用handler来处理保证线程安全
public static void buy(int pType, int money){
diamondPackType = pType;
appActivity.sendMsg(10000, money);
}
@Override
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); // this line is required for clipping
return glSurfaceView;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
appActivity = this;
}
public void sendMsg(int flag, int money){
Message msg = handler.obtainMessage();
msg.what = flag;
msg.arg1 = money;
Bundle bundle = new Bundle();
msg.setData(bundle);
msg.sendToTarget();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
Bundle b = msg.getData();
if(msg.what == 10000){
int money = msg.arg1;
//通知C++程序购买完成
appActivity.callCpp(appActivity.diamondPackType);
System.out.print(appActivity.diamondPackType + ":" + money);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment