现在的位置: 首页 > 综合 > 正文

cocos2d-x中的单例模式运用

2013年12月09日 ⁄ 综合 ⁄ 共 3194字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/we000636/article/details/8536878

我们在C++往往能用到单例模式,但在cocos2d-x中,运用单例模式时,您是否遇到了麻烦,各种“无法解析”,“error LNK"错误出来。

下面我用个例子简单介绍下单例模式在cocos2d-x是如何编写的:

这里我编写一个类Global,用来存储游戏中全局都可以访问的唯一变量,这就要求我们随时可以在任意对象中能访问Global,来读取里面存储的唯一变量

头文件如下:

  1. #ifndef _GLOBAL_H_
  2. #define _GLOBAL_H_
  3. #include "cocos2d.h"
  4. #include "StartLayer.h"
  5. class Global{
  6. public:
  7. StartLayer* startLayer; //存储全局可以访问的唯一性变量
  8. static Global* toIns(); //通过这个方法返回Global对象
  9. protected:
  10. ~Global();
  11. };
  12. #endif
  1. #ifndef _GLOBAL_H_
  2. #define _GLOBAL_H_
  3. #include "cocos2d.h"
  4. #include "StartLayer.h"
  5. class Global{
  6. public:
  7. StartLayer* startLayer; //存储全局可以访问的唯一性变量
  8. static Global* toIns(); //通过这个方法返回Global对象
  9. protected:
  10. ~Global();
  11. };
  12. #endif

Cpp文件如下:

  1. #include "Global.h"
  2. using namespace cocos2d;
  3. static Global* share=NULL;
    //这行非常重要,我们之前犯的错误就是C++习惯,将此变量声明和初始化放在头文件中,导致错误
  4. Global::~Global(void){
  5. startLayer = NULL;
  6. }
  7. Global* Global::toIns(){
  8. if(!share){
  9. share = new Global();
  10. CCLOG("first");
  11. }
  12. CCLOG("hello");
  13. return share;
  14. }
  1. #include "Global.h"
  2. using namespace cocos2d;
  3. static Global* share=NULL;
    //这行非常重要,我们之前犯的错误就是C++习惯,将此变量声明和初始化放在头文件中,导致错误
  4. Global::~Global(void){
  5. startLayer = NULL;
  6. }
  7. Global* Global::toIns(){
  8. if(!share){
  9. share = new Global();
  10. CCLOG("first");
  11. }
  12. CCLOG("hello");
  13. return share;
  14. }

也可以这样做:

头文件如下:

  1. #ifndef _GLOBAL_H_
  2. #define _GLOBAL_H_
  3. #include "cocos2d.h"
  4. #include "StartLayer.h"
  5. class Global{
  6. public:
  7. StartLayer* startLayer;
  8. static Global* toIns();
  9. static Global* share;
    //静态变量声明在这里
  10. protected:
  11. ~Global();
  12. };
  13. #endif
  1. #ifndef _GLOBAL_H_
  2. #define _GLOBAL_H_
  3. #include "cocos2d.h"
  4. #include "StartLayer.h"
  5. class Global{
  6. public:
  7. StartLayer* startLayer;
  8. static Global* toIns();
  9. static Global* share;
    //静态变量声明在这里
  10. protected:
  11. ~Global();
  12. };
  13. #endif

CPP文件如下:

  1. #include "Global.h"
  2. using namespace cocos2d;
  3. //static Global* share=NULL;
  4. Global* Global::share = NULL; //静态变量初始化放在这,而不是放在头文件中
  5. Global::~Global(void){
  6. startLayer = NULL;
  7. }
  8. Global* Global::toIns(){
  9. if(!share){
  10. share = new Global();
  11. CCLOG("first");
  12. }
  13. CCLOG("hello");
  14. return share;
  15. }
  1. #include "Global.h"
  2. using namespace cocos2d;
  3. //static Global* share=NULL;
  4. Global* Global::share = NULL; //静态变量初始化放在这,而不是放在头文件中
  5. Global::~Global(void){
  6. startLayer = NULL;
  7. }
  8. Global* Global::toIns(){
  9. if(!share){
  10. share = new Global();
  11. CCLOG("first");
  12. }
  13. CCLOG("hello");
  14. return share;
  15. }

将代码运用到你的工程中,你学会了吗?

例外你也可以参考cocos2d-x中本身的单例(例如CCDirector)里面的代码,它们是如何实例单例的

抱歉!评论已关闭.