从Java JNI调用C ++函数 - Cocos2d-x

时间:2017-09-13 17:13:19

标签: java android c++ java-native-interface google-play-services

我正在开发一个项目 Cocos2d-x(最新),其中我使用Google Play游戏服务,当我尝试将Google Play服务与C ++集成,然后我遇到了一些问题决定换到JAVA。

用户点击按钮"邀请朋友"时,方法startGame()会成功调用JNI,但什么都没发生!以下场景不显示。

C ++代码:

extern "C"
{
JNIEXPORT void JNICALL Java_sonar_systems_frameworks_GooglePlayServices_GooglePlayServicesGameHelper_startGame(JNIEnv* env, jobject thiz);
};

JNIEXPORT void JNICALL Java_sonar_systems_frameworks_GooglePlayServices_GooglePlayServicesGameHelper_startGame(JNIEnv* env, jobject thiz)
{

    MenuScene::goToScene();

}
void MenuScene::goToScene() {

    //Going to the next scene
    Scene * gameScene=GameScene::createScene();
    TransitionFade*transition=TransitionFade::create(TRANSITION_TIME, gameScene);
    //Replace the current Scene with New Game Scene
    Director::getInstance()->replaceScene(transition);
}

调用上述函数的Java代码:

private native void startGame();
public void inviteFriend() {

    startGame();

    // launch the player selection screen
    // minimum: 1 other player; maximum: 3 other players
    //Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 1);
    //((Activity) GooglePlayServices.ctx).startActivityForResult(intent, RC_SELECT_PLAYERS);

}

C ++和Java之间的通信很好,但为什么我不能去下一个场景,代码是正确的!

谢谢,

1 个答案:

答案 0 :(得分:0)

我修复了问题,好像他需要在CocosThread中执行所以我确实喜欢这样:

 Director::getInstance()->getScheduler()->performFunctionInCocosThread([&](){
        MenuScene::goToScene();
    });