在Cocos2dx中的主/UI线程上运行代码 [英] Running code on the main/UI thread in Cocos2dx
问题描述
编辑:所以问题出在Java方面.没有调用购买完成的侦听器.这非常有帮助: IabHelper PurchaseFinishedListener
So the issue was on the Java side. The purchase finished listener wasn't being called. This was very helpful: IabHelper PurchaseFinishedListener
我的Cocos2dx游戏大部分可以在Android和iOS上正常运行.只有认为给我带来麻烦的是Android应用内结算.
My Cocos2dx game runs fine on Android and iOS for the most part. Only think giving me trouble is Android In-App Billing.
我正在使用JNI从C ++调用Java. Java代码与Google Play计费系统来回往返,最终调用C ++代码,该代码指示向用户赠送多少宝藏(成功购买的金额).
I'm using JNI to call from C++ to Java. The Java code goes back and forth w/ Google Play billing system and ultimately calls back to the C++ code indicating how much treasure to give to the user (amount successfully purchased).
从Java到C ++的调用做的事情很奇怪.运行的C ++代码应该更新两件事.但是,它仅更新一个,并且不一致.同样,从Java到C ++的多次调用也会导致CCNode忽略触摸并做其他奇怪的事情.
The call from Java back to C++ is doing something very strange. The C++ code that runs should update the display of two things. However it only updates one and it's not consistent. Also multiple calls from Java to C++ result in CCNodes ignoring touches and doing other strange things.
在阅读了类似的问题后,我意识到也许Java到C ++的调用不在主线程/UI线程上.所以我试图像这样解决这个问题:
After reading up on similar issues I realized that perhaps the Java to C++ call wasn't on the main/UI thread. So I tried to fix that like this:
// Java code
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess()) {
String sku = purchase.getSku();
if (sku.equals(IAB_ID_ABC)) {
me.runOnUiThread(new Runnable() {
public void run() {
callCppMethodFromJava_giveUserABC();
}
});
}
}
};
在这里,我试图在主/UI线程上调用GiveUserABC. GiveUserABC被称为,但是它表现出如上所述的奇怪行为.
Here I'm trying to call giveUserABC on the main/UI thread. giveUserABC is called however it's exhibiting strange behavior as described above.
我尝试的另一件事是通过CCNotificationCenter在giveUserABC中发布通知.这是在黑暗中拍摄的镜头,但我读到它对某人有用.
Another thing I tried is posting a notification via CCNotificationCenter in giveUserABC. This was a shot in the dark but I read that it worked somewhere for someone.
不幸的是,这些都没有解决奇怪的问题.非常感谢您对理解和解决这种情况的任何帮助!
Unfortunately none of this has fixed the strange behavior. Any help in understanding and fixing this situation is greatly appreciated!
推荐答案
根据我的经验,您应该在GLThread上运行它:
From my experience, you should run it at GLThread:
me.runOnGLThread(new Runnable() {
public void run() {
callCppMethodFromJava_giveUserABC();
}
});
我们知道, UiThread 是Android应用程序的主线程,而Cocos2dxGLSurfaceView
通常以GL阈值运行.
As we know, the UiThread is the main thread of an Android app, while the Cocos2dxGLSurfaceView
is usually run at GL thrread.
因此,如果要从Java调用C ++,则应在GL线程中调用它.从C ++调用Java时,通常应该在Ui线程上调用它.根据我的经验,在大多数情况下都是正确的.
So if you want to call from Java to C++, you should call it at GL thread. While calling from C++ to Java, you should usually call it at Ui thread. It's right in most cases from my experience.
有关更多信息,runOnGLThread
方法不是Android的api,它是通过cocos2d-x项目中的org.cocos2dx.lib.Cocos2dxActivity.java
实现的.
For more information, runOnGLThread
method is not an api of Android, it is achieved org.cocos2dx.lib.Cocos2dxActivity.java
in cocos2d-x project.
从源代码中,您可以轻松地发现,使用Cocos2dxGLSurfaceView.getInstance().queueEvent()
代替this.runOnGLThread()
也是可以的.
From the source code, you can easily find that, using Cocos2dxGLSurfaceView.getInstance().queueEvent()
instead of this.runOnGLThread()
is also ok.
这篇关于在Cocos2dx中的主/UI线程上运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!