安卓Cocos2dX JNI桥接

Android Cocos2dX JNI Bridge

本文关键字:桥接 JNI Cocos2dX 安卓      更新时间:2023-10-16

我刚开始使用Cocos2d-X,只是在我的应用程序中尝试使用JNI。这是我的java代码

public class JNITest {
    public static native void printSomething();
    public static void printSomethingFromJava(){    
        printSomething();
    }
}

我使用javah生成一个头文件,并在MyScene.cpp文件中实现一个C函数

void notify(){
     CCNotificationCenter::sharedNotificationCenter()->postNotification("hello",NULL);
}
extern "C" {
    void Java_com_nbs_test_JNITest_printSomething(JNIEnv *, jclass){
        CCLog("THE jni call is successfull");
        notify();
    }
}

CCLog消息已经打印出来,所以我的android->c++桥正在工作。在的构造函数中MyScene.cpp我设置了一个监听器

MyScene::MyScene() {
    if (!CCLayer::init()) {
        return;
    }
     CCNotificationCenter::sharedNotificationCenter()->addObserver(
                      this,
                      callfuncO_selector( MyScene::printSomethingInCpp),
                      "hello",
                      NULL);

在MyScene::printSomethingInCpp中,我只打印这个

void MyScene::printSomethingInCpp(){
    CCLog("Its goton hererew---------------------------->");
}

PingoScreen::printSomethingInCpp中的日志消息从不打印。我不知道问题是我的JNI调用还是Observer模式?

comeplete码

#ifndef PINGOSCREEN_H_
#define PINGOSCREEN_H_
#include "RequestHandler.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "box2d.h"
#include "json.h"
#include "GLES-Render.h"
#define MY_SCREEN_RES "hello"
class PingoScreen: public cocos2d::CCLayer{
public:
    bool init();
        PingoScreen();
        ~PingoScreen();
        static cocos2d::CCScene* scene();
        CREATE_FUNC(PingoScreen);
        void printSomethingInCpp(CCObject *pObject);
        static void notify();
        static PingoScreen* getInstance();
        void setListener();
private:
        cocos2d::CCSprite *ball;
        RequestHandler* r;
};

#include "PingoScreen.h"
#include "SampleRequest.h"
#include "platform/android/jni/JniHelper.h"

#define PTM_RATIO 32;
using namespace cocos2d;
static PingoScreen* _mInstance = NULL;
CCScene* PingoScreen::scene() {
    CCScene* pScene = CCScene::create();
    PingoScreen* pingoScreen =PingoScreen::create();
    pScene->addChild(pingoScreen);
    return pScene;
}

PingoScreen* PingoScreen::getInstance(){
    if(!_mInstance){
        _mInstance=PingoScreen::create();
    }
    return _mInstance;
}


void PingoScreen::setListener(){
}

bool PingoScreen::init(){
    if (!CCLayer::init()) {
            return false;
    }
        // CCNotificationCenter::sharedNotificationCenter()->postNotification("hello",NULL);
     CCNotificationCenter::sharedNotificationCenter()->addObserver(
                 this,
                 callfuncO_selector(PingoScreen::printSomethingInCpp),
                 "hello",
                 NULL);
        CCSize pSize = CCDirector::sharedDirector()->getWinSize();
        CCSprite* backgroundSprite = CCSprite::create("room5.png");
        backgroundSprite->setAnchorPoint(CCPointZero);
        this->addChild(backgroundSprite, -1);
        return true;
}
PingoScreen::PingoScreen() {

}
PingoScreen::~PingoScreen() {
}

void PingoScreen::printSomethingInCpp(CCObject *pObject){
    CCLog("Its goton hererew 1212112----------@@@@@@@@@@@@@@@212121212@@@@@@@@@@@@@@@------------------>");
}
void PingoScreen::notify(){
    CCNotificationCenter::sharedNotificationCenter()->postNotification("hello",NULL);
}
extern "C" {
    void Java_com_nbs_test_JNITest_printSomething(JNIEnv *, jclass){
        PingoScreen::notify();
    }
}

callfuncO_selector采用接受CCObject*的方法的函数指针作为参数。

你的方法MyScene::printSomethingInCpp()应该是这样的:

MyScene::printSomethingInCpp(CCObject* pSender)
{
   // your code
}