0
我是使用Cocos2d-X的新手,並且只是在應用程序中使用JNI進行實驗。因此,這裏是我的Java代碼Android Cocos2dX JNI Bridge
public class JNITest {
public static native void printSomething();
public static void printSomethingFromJava(){
printSomething();
}
}
我用JAVAH生成一個頭文件,並在我的MyScene.cpp文件執行打印
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消息的C函數,因此我的Android - > C++橋正在工作。在 MyScene.cpp的構造函數中我設置了一個監聽
MyScene::MyScene() {
if (!CCLayer::init()) {
return;
}
CCNotificationCenter::sharedNotificationCenter()->addObserver(
this,
callfuncO_selector(MyScene::printSomethingInCpp),
"hello",
NULL);
,並在MyScene :: printSomethingInCpp我剛打印PingoScreen :: printSomethingInCpp這
void MyScene::printSomethingInCpp(){
CCLog("Its goton hererew---------------------------->");
}
日誌信息從不打印。我不知道問題出在我的JNI調用還是觀察者模式?
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;
};
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 [email protected]@@@@@@@@@@@@@@[email protected]@@@@@@@@@@@@@@------------------>");
}
void PingoScreen::notify(){
CCNotificationCenter::sharedNotificationCenter()->postNotification("hello",NULL);
}
extern "C" {
void Java_com_nbs_test_JNITest_printSomething(JNIEnv *, jclass){
PingoScreen::notify();
}
}
我已經做了仍然沒有工作。 – AndroidDev
如果我將帖子通知調用移動到Cocos2d-X代碼中的任何地方,它可以正常工作。從C代碼生成的通知沒有找到任何觀察者? – AndroidDev
用完整的代碼編輯我的問題。 – AndroidDev