CocosCreator系列教程——js與Java原生互相調用
前期先看一下鏈接: cocoscreator官方文檔中的Java原生反射機制.
1.客戶端基於2.2.1版本新建一個hello word工程
2.新建Test.js腳本,並導出爲模塊,代碼如下:
const Test = {
getVau: function (data) {
if (cc.game) {
cc.game.testSrt = data;
}
console.log('當前返回字符:', data);
}
}
module.exports = Test;
然後在HelloWorld.js腳本中引入Test模塊,在調用onBtnClick()方法時調用Java原生方法
const Test = require("Test");
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!',
},
// use this for initialization
onLoad: function () {
this.label.string = this.text;
if (!cc.game) {
cc.game = {};
cc.game.Test = Test;
}
},
onBtnClick() {
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getS", "()V");
this.label.string = cc.game.testSrt;
},
});
其中game爲掛載在cc下的一個對象
這時js代碼到此完畢,接下來是AppActivity.java中編輯代碼
1.聲明當前AppActivity並初始化:
private static AppActivity app = null;
2.聲明要存儲起來的字符串變量
public static String testStr= "測試字符串";
3.在onCreate()方法中爲當前AppActivity賦值
app = this;
4.交互方法(要寫成靜態公開的方法)在js客戶端調用此方法時,Java原生端調用js中的getVau(),並傳遞參數
注意:Cocos2dxJavascriptJavaBridge.evalString裏面的參數在新版本中改成序列化傳參,如下所示:
public static void getS(){
app.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString(String.format("cc.app.Test.getVau(\"%s\")",testStr));
}
});
}
至此全部完成,用你的Android studio工程打開項目真機調式就可以看到你打印的log了,因爲新版本跟老版本的交互方法參數傳遞有區別,如果creator是1.x版本,請使用:Cocos2dxJavascriptJavaBridge.evalString(“cc.app.Test.getVau(‘”+ testStr +"‘)");這種方式傳遞
AppActivity.java代碼如下:
package org.cocos2dx.javascript;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.content.Intent;
import android.content.res.Configuration;
public class AppActivity extends Cocos2dxActivity {
private static AppActivity app = null;
public static String Absd = "132132132";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// DO OTHER INITIALIZATION BELOW
SDKWrapper.getInstance().init(this);
app = this;
}
public static void getS(){
app.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString(String.format("cc.app.Test.getVau(\"%s\")",Absd));
}
});
}
@Override
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// TestCpp should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
SDKWrapper.getInstance().setGLSurfaceView(glSurfaceView, this);
return glSurfaceView;
}
@Override
protected void onResume() {
super.onResume();
SDKWrapper.getInstance().onResume();
}
@Override
protected void onPause() {
super.onPause();
SDKWrapper.getInstance().onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
SDKWrapper.getInstance().onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
SDKWrapper.getInstance().onNewIntent(intent);
}
@Override
protected void onRestart() {
super.onRestart();
SDKWrapper.getInstance().onRestart();
}
@Override
protected void onStop() {
super.onStop();
SDKWrapper.getInstance().onStop();
}
@Override
public void onBackPressed() {
SDKWrapper.getInstance().onBackPressed();
super.onBackPressed();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
SDKWrapper.getInstance().onConfigurationChanged(newConfig);
super.onConfigurationChanged(newConfig);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
SDKWrapper.getInstance().onRestoreInstanceState(savedInstanceState);
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
SDKWrapper.getInstance().onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
protected void onStart() {
SDKWrapper.getInstance().onStart();
super.onStart();
}
}