所以,我正在关注一本Android版的iOS &的书。我在OS X上使用cocos2d-x v2.2
10.8扩展C++类时的错误(使用cocos2d-x框架)
我在扩展CCSprite
类时遇到了问题。每当我从这个类创建一个对象时,该项目就停止在Android上运行。 Eclipse拒绝构建它。我已经缩小了这个问题(如下所述),但我不知道如何解决这个问题。
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;
}
...
现在,在我的HelloWorld.cpp
文件,我如下创建我GameSprite
类的一个实例:
这使得Eclipse来抛出一个错误
GameSprite * player1;
player1 = GameSprite::gameSpriteWithFile("myPic.png");
player1->setPosition(ccp(_screenSize.width*0.5, player1->radius() * 2));
this->addChild(player1);
这在iOS中完美无瑕。精灵会在正确的位置添加到屏幕上。但是当我尝试使用Eclipse为android构建此项目时,出现错误消息:“您的项目包含错误,请在运行应用程序之前修复它们”。
当我从我的HelloWorld.cpp
文件中删除这段代码(上述)时,该应用程序在Android上运行时没有任何问题。
据我所知,GameSprite.cpp
文件中的GameSprite :: gameSpriteWithFile方法存在问题。但我无法弄清楚有什么问题,我怎么去解决它。
任何帮助家伙?
感谢
Eclipse报告的确切错误是什么?你确定你正在构建GameSprite.cpp作为你的项目的一部分吗? – simonc
这就是确切的信息:*“您的项目包含错误,请在运行您的应用程序之前修复它们”*。 当我删除'GameSprite * player1'和以下几行时,它就消失了。然后项目成功建立。奇怪的权利? – wiseindy
好的,我检查了控制台。这些都是它引发错误消息: 'JNI /../../类/ HelloWorldScene.cpp:91:错误:未定义参照 'GameSprite ::半径()'' 和 'JNI /。 ./../Classes/HelloWorldScene.cpp:94:error:undefined引用'GameSprite :: gameSpriteWithFile(char const *)'' – wiseindy