Accessing Cocos2d Layer from main.cpp

Accessing Cocos2d Layer from main.cpp
0.0 0


I am calling a native C++ function from Java using JNI. This is working fine. The problem I am having is then accessing a CCLayer from there.


`void Java_com_mycompany_testproject_TestProject_testFunc(JNIEnv *env, jobject thiz)
__android_log_print(ANDROID_LOG_DEBUG, “JNI-C++”, “testFunc”);

    TestProject* testProject = (TestProject* ) (CCDirector::sharedDirector()->getRunningScene());
    testProject-> ** PROBLEM HERE **

No available functions are coming up to access. As if *testProject was never casted to anything. By default at the top of main.cpp there is the line: #include "TestProject.h"

How do I call functions from main.cpp to TestProject.h or something similar like HelloWorldScene.h


If you followed the HelloWorld template, TestProject* is most likely a CCLayer.
You could:

  1. On the TestProject::scene() method, give a tag to the layer, say **setTag*
  2. Then on that JNI function, get the current scene and use**getChildbyTag( 15 )* to get the current layer.


The setup is exactly like HelloWorldScene. The issue is not so much finding the correct layer (thanks for the tag fix), the issue is the casted object is not being recognized in the file while coding and then when I run the file i get a fatal signal 11. It’s like main.cpp is not recognizing TestProject.h or any of its functions.


#ifndef __TestProject_H__
#define __TestProject_H__

#include "cocos2d.h"

class TestProject : public cocos2d::CCLayer
        virtual bool init();  

        static cocos2d::CCScene* scene();

        static void testLinkage();
        void testLinkageInternal();




TestProject* testProject = dynamic_cast(cocos2d::CCDirector::sharedDirector()->getRunningScene()->getChildByTag(15));

When you start typing testProject-> … Neither functions come up here to be accessed. How do I get main.cpp to recognize functions in TestProject.h


Take a look at this part:

class TestProject : public cocos2d::CCLayer

You are subclass CCLayer, not CCScene.

CCDirector::sharedDirector()->getRunningScene() returns a CCScene object so you can’t cast it to TestProject* as TestProject is a subclass of CCLayer.

As for the functions, you are using a static method. TestProject::testLinkage* is a static method. You don’t have to use the>* accessor to invoke it. All you have to do is include the TestProject.h file in main.cpp and invoke it by typing TestProject::testLinkage. Like so:
#include “TestProject.h”
void Main::doSomething {
// Call static method from TestProject class.
// Get Test project instance.
// TIP: I suggest you divide this line into two. It makes it more readable.
cocos2d::CCScene * currentScene = cocos2d::CCDirector::sharedDirector
> getRunningScene();
TestProject * testProject = ( TestProject * ) currentScene > getChildByTag;
// Invoke non-static method in TestProject instance
> invokeNonStaticMethod();


I get what you are saying about not being able to cast the scene object as layer etc. The issue is that even just typing TestProject::testLinkage() does not work. Typing TestProject:: ** - Once the menu pops up there is no option for the testLinkage. What I’m getting at is that it seems like its ignoring that TestProject.h is included or something.

For instance I can hop into AppDelegate.cpp and easily type

#include “TestProject.h”

and it is recognized but for some reason not in main.cpp and I am trying to figure out why this is.


It’s either your IDE is hating you right now or the main.cpp file is somewhere else and not properly linked in your project. You might want to do some restarting or something. This happened to me some times too but restarting the machine does the trick most of the time. You could also try to Clean your project (in XCode it’s Product > Clean or command shift K )


Maybe thats the issue, what would be the proper way to link this to the xCode project. I was just double clicking it and editing it. I am not really on top of certain files get utilized in terms of what needs to be where for android/ios/cocos2dx to work together nicely. snippet

LOCAL_SRC_FILES := testproject/main.cpp \
                   ../../Classes/AppDelegate.cpp \


Tried moving main.cpp to Classes and importing it into xcode but doesn’t seem to do anything.


Actually now it is working, the IDE is not recognizing anything but it compiles and the functions are being called. Thanks for the help.