简体   繁体   English

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

[英]Calling a C++ function from Java JNI - Cocos2d-x

I am working on a project in Cocos2d-x (Latest) in which I worked with Google Play Game Services, I encountered several problems when I tried to integrate Google Play Services with C++ and then I decided to change to JAVA. 我正在Cocos2d-x(最新)中的一个项目中工作,该项目是我与Google Play游戏服务一起工作的,当我尝试将Google Play服务与C ++集成时遇到了几个问题,然后我决定改用JAVA。

When the user click to the Button "Invite Friend", the method startGame() is called successfully with JNI, but nothing happening ! 用户单击按钮 “邀请朋友”时,使用JNI成功调用了startGame()方法 ,但没有任何反应! The following scene does not display. 以下场景不显示。

C++ Code : 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 Code from where the above function is called : 调用上述函数的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);

}

The communication between C++ and Java is fine, but why I cannot to go to next scene, the code is correct ! C ++和Java之间的通信很好,但是为什么我不能下一个场景,代码是正确的!

Thank you, 谢谢,

I fixed the problem, It seems like he needs to execute in CocosThread so I did like that : 我解决了这个问题,看来他需要在CocosThread中执行,所以我做到了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM