0

So, I was following a tutorial from a book for an Android & iOS game. I am using cocos2d-x v2.2 on OS X 10.8

I came across a problem while extending the CCSprite class. Whenever I create an object from this class, the project stops running on Android. Eclipse refuses to build it. I have narrowed down the problem (as described below), but I have no idea how to fix it.

GameSprite.h

#ifndef __GAMESPRITE_H__
#define __GAMESPRITE_H__
#include "cocos2d.h" 

using namespace cocos2d;

class GameSprite : public CCSprite {
public:

    ...

    GameSprite(void);
    ~GameSprite(void);

    static GameSprite* gameSpriteWithFile (const char* pszFileName);
    virtual void setPosition(const CCPoint &pos);
    float radius();

};

#endif // __GAMESPRITE_H__

GameSprite.cpp

#include "GameSprite.h" 

GameSprite::GameSprite(void) {
    _vector = ccp(0,0);
}

GameSprite::~GameSprite(void) {

}

GameSprite* GameSprite::gameSpriteWithFile(const char* pszFileName) {
    GameSprite* sprite = new GameSprite();
    if (sprite && sprite->initWithFile(pszFileName)) {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return NULL;
}

...

Now, in my HelloWorld.cpp file, I create an instance of my GameSprite class as follows:

This causes Eclipse to throw an error

GameSprite * player1;
player1 = GameSprite::gameSpriteWithFile("myPic.png");
player1->setPosition(ccp(_screenSize.width*0.5, player1->radius() * 2));
this->addChild(player1);

This works flawlessly in iOS. The sprite gets added to the screen at the right position. But when I try to build this project for android using Eclipse, I get an error message saying that: "Your project contains error(s). Please fix them before running your application".

When I remove this chunk of code (above) from my HelloWorld.cpp file, the app runs on Android without any issues.

As far as I feel, there is an issue with the GameSprite::gameSpriteWithFile method in the GameSprite.cpp file. But I cannot figure out what is the problem and how could I go about fixing it.

Any help guys?

Thanks

wiseindy
  • 19,434
  • 5
  • 27
  • 38
  • What is the exact error that Eclipse reports? Are you sure you're building GameSprite.cpp as part of your project? – simonc Oct 19 '13 at 17:46
  • This it the exact message: *"Your project contains error(s). Please fix them before running your application"*. It goes away when I remove `GameSprite * player1` and the following lines. Then the project builds successfully. Weird right? – wiseindy Oct 19 '13 at 18:09
  • Okay, I checked the console. These are the error messages it throws: `jni/../../Classes/HelloWorldScene.cpp:91: error: undefined reference to 'GameSprite::radius()'` and `jni/../../Classes/HelloWorldScene.cpp:94: error: undefined reference to 'GameSprite::gameSpriteWithFile(char const*)'` – wiseindy Oct 19 '13 at 18:20
  • 1
    I fixed the problem, simonc. You were right. GameSprite.cpp wasn't building as a part of the project. Thanks :) – wiseindy Oct 19 '13 at 18:25

1 Answers1

0

I solved it thanks to simonc. Thanks! :) I had to add my GameSprite.cpp class to the Android.mk file. (jni/hellocpp/Android.mk)

Eclipse couldn't find my newly created class hence it wasn't building it.

Community
  • 1
  • 1
wiseindy
  • 19,434
  • 5
  • 27
  • 38