Android的Cocos2dX JNI桥 [英] Android Cocos2dX JNI Bridge

查看:174
本文介绍了Android的Cocos2dX JNI桥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来使用的Cocos2D-X和我只是在我的应用程序中使用JNI尝试。因此,这里是我的Java code

 公共类JNITest {
    公共静态本地无效printSomething();    公共静态无效printSomethingFromJava(){
        printSomething();
    }}

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

 无效通知(){
     CCNotificationCenter :: sharedNotificationCenter() - GT; postNotification(你好,NULL);
}为externC{
    无效Java_com_nbs_test_JNITest_printSomething(JNIEnv的*,JCLASS){
        CCLOG(下称JNI调用是全成);
        通知();
    }
}

将打印CCLOG消息,所以我的机器人 - > C ++桥正在工作。在构造函数中
MyScene.cpp我建立了一个侦听器

  MyScene :: MyScene(){    如果(!CCLayer ::的init()){
        返回;
    }
     CCNotificationCenter :: sharedNotificationCenter() - >的addObserver(
                      这个,
                      callfuncO_selector(MyScene :: printSomethingInCpp)
                      你好,
                      空值);

和在MyScene :: printSomethingInCpp我只是打印此

 无效MyScene :: printSomethingInCpp(){
    CCLOG(它goton hererew ---------------------------->);
}

是从来没有印在PingoScreen :: printSomethingInCpp的日志信息。我不知道问题是否与我的JNI调用或Observer模式?

comeplete code

 的#ifndef PINGOSCREEN_H_
#定义PINGOSCREEN_H_
#包括RequestHandler.h
#包括cocos2d.h
#包括茯苓,ext.h
#包括box2d.h
#包括json.h
#包括GLES-Render.h#定义MY_SCREEN_RES你好
类PingoScreen:公众的cocos2d :: CCLayer {
上市:
    布尔的init();
        PingoScreen();
        〜PingoScreen();
        静态的cocos2d :: CCScene *场景();
        CREATE_FUNC(PingoScreen);
        无效printSomethingInCpp(CCObject * pObject);
        静态无效的通知();
        静态PingoScreen *的getInstance();        无效使用setListener();
私人的:
        cocos2d的:: CCSprite *球;
        RequestHandler * R;};

 的#includePingoScreen.h
#包括SampleRequest.h
#包括平台/安卓/ JNI / JniHelper.h
#定义PTM_RATIO 32;
使用cocos2d的命名空间;
静态PingoScreen * _mInstance = NULL;CCScene * PingoScreen ::场景(){
    CCScene * pScene = CCScene ::创建();
    PingoScreen * pingoScreen = PingoScreen ::创建();
    pScene->的addChild(pingoScreen);
    返回pScene;
}PingoScreen * PingoScreen ::的getInstance(){
    如果(!_ mInstance){
        _mInstance = PingoScreen ::创建();
    }
    返回_mInstance;
}
无效PingoScreen ::使用setListener(){
}
布尔PingoScreen ::的init(){
    如果(!CCLayer ::的init()){
            返回false;
    }        // CCNotificationCenter :: sharedNotificationCenter() - GT; postNotification(你好,NULL);
     CCNotificationCenter :: sharedNotificationCenter() - >的addObserver(
                 这个,
                 callfuncO_selector(PingoScreen :: printSomethingInCpp)
                 你好,
                 空值);        CCSize pSize = CCDirector :: sharedDirector() - > getWinSize();
        CCSprite * backgroundSprite = CCSprite ::创建(room5.png);
        backgroundSprite-> setAnchorPoint(CCPointZero);
        这 - >的addChild(backgroundSprite,-1);
        返回true;
}
PingoScreen :: PingoScreen(){
}PingoScreen ::〜PingoScreen(){}
无效PingoScreen :: printSomethingInCpp(CCObject * pObject){
    CCLOG(它goton hererew 1212112 ---------- @@@@@@@@@@@@@@@ 212121212 @@@@@@@@@@@@@@@ - ---------------->中);
}无效PingoScreen ::通知(){
    CCNotificationCenter :: sharedNotificationCenter() - GT; postNotification(你好,NULL);
}为externC{
    无效Java_com_nbs_test_JNITest_printSomething(JNIEnv的*,JCLASS){
        PingoScreen ::通知();
    }
}


解决方案

callfuncO_selector 取方法的函数指针,接受 CCObject * 作为参数。<​​/ p>

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

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

I am new to using Cocos2d-X and am just experimenting using JNI in my application. So here is my java code

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

    public static void printSomethingFromJava(){    
        printSomething();
    }

}

I use javah to generate a header file, and implement a C function in my MyScene.cpp file

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();
    }
}

The CCLog message is printed, so my android -> c++ bridge is working. In the constructor of MyScene.cpp i set up a Listener

MyScene::MyScene() {

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

and in the MyScene::printSomethingInCpp i just print this

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

The log message in PingoScreen::printSomethingInCpp is never printed. I dont know if the problem is with my JNI call or with the Observer pattern ?

comeplete code

#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;

};

AND

#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 takes function pointer of method which accept CCObject* as argument.

Your method MyScene::printSomethingInCpp() should be like this:

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

这篇关于Android的Cocos2dX JNI桥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆