Android label black rect after jni call

MyGame is built with cocos2dx 2.2.6 and I’m having a process in java when calling C++ delegate using jni and trying to update label string it become a black rect even if I’m calling the delegate within android ui thread.
What I usually do to make it work is to make a CCCallFunc and then runAction

 CCCallFunc* func = CCCallFunc::create(this, callfunc_selector(MyLayer::updateProgress));
 this->runAction(func);

However it’s repeated case and I have to edit many places is there any better way to get it done?

Screenshot_1550741407

You should call C++ code from OpenGL thread.

Cocos2dxHelper.runOnGLThread(new Runnable() {
                            @Override
                            public void run() {
                                // put your call to C++ here
                            }
                        });

Thank you @dimon4eg runOnGLThread is a member of Cocos2dxHelperListener How can I use it?

add
import org.cocos2dx.lib.Cocos2dxHelper;

I did that however I’m getting error cannot resolve method runOnGLThread I see that it is declared under Cocos2dxHelperListener not Cocos2dxHelper so shall I implement this listener in my java code?

I find out that we can do so using Cocos2dxActivity I will try it thank you

Thank you so much this work for me. You made my day :+1:

Cocos2dxActivity main = (Cocos2dxActivity) MyApplication.getCurrentAcivity();
main.runOnGLThread(new Runnable() {
     @Override
      public void run() {
            // put your call to C++ here
             }
         });

Glad to help you :wink:

1 Like

I forgot to mention that I’m using cocos2dx 2.2.6. but current activity become null when app in background. so I will never getting my delegate called if the app in background. rather than I have to edit Cocos2dxHelper.java and add the needed method as

public static void runOnGLThread(final Runnable r) {
    Cocos2dxHelper.sCocos2dxHelperListener.runOnGLThread(r);
}

@dimon4eg This Solve the problem when the app in foreground. But I still have the same issue if the delegate called when the app in background even if it is called from GLThread. Do you have any suggestion?

I don’t know why activity becomes null in cocos2dx 2.2.6.
But why do you need to call something in background?

It is a download process and I’m updating progress and other buttons depending on the state. For the currentActivity it is not a problem. I found similar issue in cocos2dx v3. I will check if I could apply the changes to cocos2dx 2. Thanks for your time.

I’m using CCLabelTTF. The structure is so different in v3. I can’t solve this.

can you check is your app is background then mark some flag. After app goes to foreground check the flag and call that code.
Maybe this will fix your issue.

Problem solved by performFunctionInCocosThread:

  1. Updating CCScheduler.h
  2. Updateding CCScheduler.cpp
  3. Calling my delegate within
    CCDirector::sharedDirector()->getScheduler() ->performFunctionInCocosThread([=]() { // Call delegate her });

Is that the one by sonarsystems?