4

Looking for some help,Iam new to cocos2dx and having error.iam using Eclipse IDE

inside HelloWorld.cpp iam doing this:

_backgroundNode = CCParallaxNodeExtras::node();

and it gives me undefined reference error which is as follows

undefined reference to 'CCParallaxNodeExtras::node()'

My CCParallaxNodeExtras.h header file code is as follows it inherits CCParallaxNode

using namespace cocos2d;
#include "cocos2d.h"

class CCParallaxNodeExtras : public cocos2d::CCParallaxNode {

    public :

    // Need to provide a constructor
    CCParallaxNodeExtras();

    // just to avoid ugly later cast and also for safety
    static CCParallaxNodeExtras* node();

    // Facility method (it’s expected to have it soon in COCOS2DX)
    void incrementOffset(CCPoint offset, CCNode* node);
};

#endif

here is the CCParallaxNodeExtras.cpp

#include "CCParallaxNodeExtras.h"
using namespace cocos2d;

// Hack to access CCPointObject (which is not a public class)
class CCPointObject  : cocos2d::CCObject {
    CC_SYNTHESIZE(cocos2d::CCPoint, m_tRatio, Ratio)
    CC_SYNTHESIZE(cocos2d::CCPoint, m_tOffset, Offset)
    CC_SYNTHESIZE(cocos2d::CCNode *, m_pChild, Child)   // weak ref
};

// Need to provide a constructor
CCParallaxNodeExtras::CCParallaxNodeExtras() {
    cocos2d::CCParallaxNode(); // call parent constructor
}

CCParallaxNodeExtras* CCParallaxNodeExtras::node() {
    return new CCParallaxNodeExtras::CCParallaxNode();
}

void CCParallaxNodeExtras::incrementOffset(cocos2d::CCPoint offset,CCNode *node){
    for( unsigned int i = 0; i < m_pParallaxArray->num; i++) {
        CCPointObject *point = (CCPointObject *)m_pParallaxArray->arr[i];
        CCNode *curNode = point->getChild();
        if( curNode->isEqual(node) ) {
            point->setOffset( ccpAdd(point->getOffset(), offset) );
            break;
        }
    }
}

Please Reply, I Know there is a lot of code above but i want to know if iam doing anything wrong.Any Help or suggesstion will be appreciated.Thanks!

Regards, Muhammad Tahir Ashraf

M.Tahir Ashraf
  • 222
  • 5
  • 15

2 Answers2

15

You must add the reference of the new cpp file into the Android.mk of the corresponding jni directory.

In my case, the "Android.mk" file is in the route: {PROJ_DIRECTORY}\proj.android\jni

Edit this file, and add the reference to your CCParallaxNodeExtras cpp as follows:

In the LOCAL_SRC_FILES section you currently have:

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp

Now include the CCParallasNodeExtras.cpp . It should look as follows:

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp  \
                   ../../Classes/CCParallaxNodeExtras.cpp

This should solve the problem. Build and run.

Noru
  • 161
  • 4
  • 4
    OR, just add `CLASSES_FILES := $(CLASSES_FILES:$(LOCAL_PATH)/%=%)` then add `LOCAL_SRC_FILES += $(CLASSES_FILES)` Never will have to manually add again. Learned from here: http://stackoverflow.com/a/20485971/1114457 – bad_keypoints Dec 30 '13 at 02:00
1

It looks like you have problem in your definition of CCParallaxNodeExtras::node() method. It should be like:

CCParallaxNodeExtras* CCParallaxNodeExtras::node() {
    return new CCParallaxNodeExtras();
}

I think it should solve the problem. Let me know if it doesn't.