cocos2d-x中的单例模式运用

转自: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
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#include "cocos2d.h"
#include "StartLayer.h"

class Global{
public:
	StartLayer* startLayer; //存储全局可以访问的唯一性变量
	static Global* toIns(); //通过这个方法返回Global对象
protected:
	~Global();
};

#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. }
#include "Global.h"

using namespace cocos2d;
static Global* share=NULL; //这行非常重要,我们之前犯的错误就是C++习惯,将此变量声明和初始化放在头文件中,导致错误
Global::~Global(void){
	startLayer = NULL;
}
Global* Global::toIns(){
	if(!share){
		share = new Global();
		CCLOG("first");
	}
	CCLOG("hello");
	return share;
}


也可以这样做:

头文件如下:

  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
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#include "cocos2d.h"
#include "StartLayer.h"

class Global{
public:
	StartLayer* startLayer;
	static Global* toIns();
	static Global* share;  //静态变量声明在这里
protected:
	~Global();
};

#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. }
#include "Global.h"

using namespace cocos2d;
//static Global* share=NULL;
Global* Global::share = NULL;  //静态变量初始化放在这,而不是放在头文件中
Global::~Global(void){
	startLayer = NULL;
}
Global* Global::toIns(){
	if(!share){
		share = new Global();
		CCLOG("first");
	}
	CCLOG("hello");
	return share;
}


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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值