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
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.