cocos2d-x安卓系统中的Singleton
Singleton in cocos2d-x Android
我正在尝试编写一个用于维护游戏数据的singleton类,它被称为GameManager,就像《学习椰子2d》一书一样。
这是我的.h文件:
#ifndef GameManager_h
#define GameManager_h
#include "cocos2d.h"
class GameManager
{
private:
//Constructor
GameManager();
//Instance of the singleton
static GameManager* m_mySingleton;
public:
//Get instance of singleton
static GameManager* sharedGameManager();
//A function that returns zero "0"
int ReturnZero(){return 0;}
// another test function
void runScene() { CCLOG("test");};
};
这是我的.cpp文件:
#include "SimpleAudioEngine.h"
#include "GameManager.h"
using namespace cocos2d;
using namespace CocosDenshion;
//All static variables need to be defined in the .cpp file
//I've added this following line to fix the problem
GameManager* GameManager::m_mySingleton = NULL;
GameManager::GameManager()
{
}
GameManager* GameManager::sharedGameManager()
{
//If the singleton has no instance yet, create one
if(NULL == m_mySingleton)
{
//Create an instance to the singleton
m_mySingleton = new GameManager();
}
//Return the singleton object
return m_mySingleton;
}
以下是HelloWorld.cpp中的呼叫:
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) {
CCLOG("return zero:%d",GameManager::sharedGameManager()->ReturnZero()); // Line 231
GameManager::sharedGameManager()->runScene(); // Line 232
}
这是一个奇怪的问题,它与xcode配合得很好,可以在iPhone上构建。但是当我尝试使用ndk构建时:
./obj/local/armeabi/objs-debug/game_logic/HelloWorldScene.o: In function `HelloWorld::ccTouchesEnded(cocos2d::CCSet*, cocos2d::CCEvent*)':
/Users/abc/Documents/def/def/android/jni/../../Classes/HelloWorldScene.cpp:232: undefined reference to `GameManager::sharedGameManager()'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libgame_logic.so] Error 1
如果未定义对"GameManager::sharedGameManager()"的引用,为什么第一个调用有效?
任何帮助都可以,谢谢!
您确定已将带有GameManager实现的cpp文件(您称之为"这是我的.cpp文件")包含在Android.mk文件中吗?
相关文章:
- C++,系统无法执行指定的程序
- C++ Singleton - Prevent ::instance() to variable
- 在UNIX系统中使用DIR查找文件的字节大小
- 错误处理.将系统错误代码映射到泛型
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在C++游戏中与库存系统作斗争
- 文件系统:复制功能的速度秘诀是什么
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 在gtest.中使用fff.h模拟系统API
- 如何制作无限制照明系统
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 如何传递多个 std::文件系统选项?
- 遍历顺序由 std::文件系统directory_iterator给出
- C++系统找不到指定的文件错误
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- libstdc++ 文件系统中未初始化的用法?
- 如何在ECS框架中更新组件数据和通知系统
- boost::文件系统::recursive_directory_iterator多线程安全
- cocos2d-x安卓系统中的Singleton