https://teratail.com/questions/71591に
以前質問させていただいたのですが、文字列はうまく言ったのですが、数字がうまく行きません。
Android側callbackで以下のように記述して、
java
1 SampleInit[] sampleInits = new SampleInit[2]; 2 for (int i = 0; i < 2; i++) { 3 SampleInit sampleInit = new SampleInit(); 4 sampleInit.name = "hoge" + i; 5 sampleInit.level = 10; 6 sampleInits[i] = sampleInit; 7 } 8 SampleBridge.cocosOnSampleInitsLoaded(sampleInits); 9
cocos側callbackでこのようにし
cpp
1JNIEXPORT void JNICALL Java_com_example_helloworld_SampleBridge_cocosOnSampleInitsLoaded(JNIEnv* env, jobject thiz, jobjectArray jsampleInits) { 2 std::vector<CCSampleInit*> sampleInits; 3 SampleDelegate* listener = NativeLauncher::getDelegate(); 4 if (listener != NULL) { 5 int cntSampleInit = env->GetArrayLength(jsampleInits); 6 for (int i = 0; i < cntSampleInit; i++) { 7 jobject jsampleInitElement = env->GetObjectArrayElement(jsampleInits, i); 8 jclass jsampleInit = env->FindClass("com/example/helloworld/SampleInit"); 9 jfieldID fidName = env->GetFieldID(jsampleInit, "name", "Ljava/lang/String;"); 10 jfieldID fidLevel = env->GetFieldID(jsampleInit, "level", "I"); 11 jstring jName = (jstring)env->GetObjectField(jsampleInitElement, fidName); 12 jint level = (jint)env->GetObjectField(jsampleInitElement, fidLevel); 13 const char* utfName = env->GetStringUTFChars(jName, 0); 14 CCSampleInit* sampleInit = new CCSampleInit(); 15 sampleInit->name = utfName; 16 sampleInit->level = (int)level; 17 sampleInits.push_back(sampleInit); 18 } 19 listener->onSampleInitsLoaded(sampleInits); 20 } 21
最終的なcocos側で以下のように受け取ったのですが、・
void HelloWorld::onSampleInitsLoaded(std::vector<CCSampleInit*> sampleInits) { log("CCSampleInit name[0]=%s", sampleInits[0]->name); log("CCSampleInit level[0]=%d", sampleInits[0]->level); log("CCSampleInit name[1]=%s", sampleInits[1]->name); log("CCSampleInit level[1]=%d", sampleInits[1]->level); }
以下の通り数字に10が入ってきませんでした。
04-10 15:40:39.087 17208-21180/com.example.helloworld D/cocos2d-x debug info: CCSampleInit name[0]=hoge0 04-10 15:40:39.087 17208-21180/com.example.helloworld D/cocos2d-x debug info: CCSampleInit level[0]=488636437 04-10 15:40:39.087 17208-21180/com.example.helloworld D/cocos2d-x debug info: CCSampleInit name[1]=hoge1 04-10 15:40:39.087 17208-21180/com.example.helloworld D/cocos2d-x debug info: CCSampleInit level[1]=488636453
AndroidのSampleInit
java
1public class SampleInit { 2 public String name = ""; 3 public int level = 0; 4}
cocosのCCSampleInit
cpp
1class CCSampleInit 2{ 3public: 4 const char* name = ""; 5 int level = 0; 6};
jintからintへも変換してますし、どこがおかしいのでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/10 07:20 編集