一段例子
//!签到请求
void FBSigninLayer::NetRequestForSign(void){
MessageBoxLayer::getMessageBoxPoint()->createWithLoading();
//为签到界面建立一个全局的观察者(CCNotificationCenter是全局的 可以通过DAY_SIGN_REQUEST(一个字符串的宏)得到观察者 ) 可以获取签到界面的状态 在一定条件下触发callbackForSign
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
callfuncO_selector(FBSigninLayer::callbackForSign),
DAY_SIGN_REQUEST, NULL);
///////////
//!玩家签到请求
DEFINE_MSG_1(cs_msg_user_sign_in_req, CS_CMD_USER_SIGN_IN_REQ,
short, nMonthDay //!签到日期
);
DEFINE_MSG_1 是一个宏 末尾_1表示一个变量的结构体 这个宏的作用就是定义个结构体并关联一个枚举值
CS_CMD_USER_SIGN_IN_REQ是一个枚举变量 服务器通过它操作的cs_msg_user_sign_in_req
/////////////
DEFINE_MSG_1(cs_msg_user_sign_in_req, CS_CMD_USER_SIGN_IN_REQ,
short, nMonthDay //!签到日期
);
DEFINE_MSG_1 是一个宏 末尾_1表示一个变量的结构体 这个宏的作用就是定义个结构体并关联一个枚举值
CS_CMD_USER_SIGN_IN_REQ是一个枚举变量 服务器通过它操作的cs_msg_user_sign_in_req
/////////////
cs_msg_user_sign_in_req req;
req.nMonthDay = m_selectDay;
//声明一个二进制的数据流变量
BinaryWriteStream clStream;
//req->数据流 之后传给服务器
clStream << req;
NetController::GetInstance()->SendMessage(clStream);
}
//!签到请求返回
void FBSigninLayer::callbackForSign(CCObject *obj)
{
//观察者任务完成(任务就是在条件触发时执行回调) 取消绑定
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, DAY_SIGN_REQUEST);
////////
template <typename DT>
class NetCallBackModel : public CCObject
{
public:
DT result;
};
//!玩家签到返回
DEFINE_MSG_2(sc_msg_user_sign_in_ret, SC_CMD_USER_SIGN_IN_RET,
int, nRet,
vector<short>, vRewardInfo //!奖励信息 0标识奖励不可领取,1标识奖励可领取,2标识奖励已领取
);
///////NetCallBackModel<sc_msg_user_sign_in_ret> * ret = (NetCallBackModel<sc_msg_user_sign_in_ret> *)obj;
///////////
enum
{
RET_SUCCESS = 0, //!操作成功
RET_FAILED = 1, //!操作失败
RET_DATA_ERROR, //!服务器数据有误
RET_REQ_DATA_ERROR, //!请求参数有误
RET_LOGIN_FIRST, //!请先登陆
RET_USER_INFO_EMPTY, //!无此玩家数据,请注册
RET_FEED_CARD_EXCEED, //!饲料卡数量超过上限
RET_FEED_CARD_NOT_FOUND, //!饲料球员卡未找到
RET_MASTER_CARD_NOT_FOUND, //!主球员卡未找到
RET_MASTER_CARD_NOT_FIT, //!主球员卡品质不满足条件
RET_MASTER_CARD_EXCEED, //!主球员卡可突破次数超过上限
。。。。。。。。。。。
///////////////
if (RET_SUCCESS == ret->result.nRet) //以下前端操作{
//从xml中得到当天所得奖励
map<string, qiandaoRewardXMLModel>::iterator model = qiandaoRewardXMLModel::qiandaoRewardModel.find(CCString::createWithFormat("%d", m_selectDay)->getCString());////////////////
class qiandaoRewardXMLModel {
public:
static map<string, qiandaoRewardXMLModel> qiandaoRewardModel;
string days; //天数
string name1; //奖励名称1
string typeId1; //奖励类型1
string number1; //数量
void initModel(string days, string name1, string typeId1, string number1);
};
/////////////
MessageBoxLayer::getMessageBoxPoint()->callMessageBoxRemove();
{
switch (atoi(model->second.typeId1.c_str())) {
case 1: //钻石
{
CCLog("?????????? 获取钻石 %s", model->second.number1.c_str());
GameUser::GetGameUser()->coupon += atoi(model->second.number1.c_str());
}
break;
case 2: //资金
{
GameUser::GetGameUser()->gold += atoi(model->second.number1.c_str());
}
break;
case 3: //训练卡
{
GameUser::GetGameUser()->trainCard += atoi(model->second.number1.c_str());
}
break;
case 10: //幸运球
{
GameUser::GetGameUser()->friendPoint += atoi(model->second.number1.c_str());
}
break;
case 11: //阵型点
{
GameUser::GetGameUser()->fightPoint += atoi(model->second.number1.c_str());
}
break;
default: //其它类型有提供的话,本接口需要求改, 如果是卡牌的话需要后端提供卡牌数据,行动力需要另加请求,道具的话需要另外统计
break;
}
//顶部用户数据立即更新
MainTopLayer::getMainTopPoint()->refreshUserContent(); //refresh 更新
//签到信息更新 2为已签到
GameUser::GetGameUser()->signInfo[m_selectDay-1] = 2;refrashScrollInfoLayer();
//!礼品
DEFINE_STRUCT_2(sc_gift_info_t,
int, type,
int, num
);
typedef std::vector<sc_gift_info_t> sc_gift_info_vt;
//
sc_gift_info_vt data;
sc_gift_info_t prize;prize.num = atoi(model->second.number1.c_str());
prize.type = atoi(model->second.typeId1.c_str());
data.push_back(prize);
//创建礼品领取成功弹框
MessageBoxLayer::getMessageBoxPoint()->createGiftBag(data, true);
}
// GameUser:: vector<short> rewardInfo; //全勤奖励信息
GameUser::GetGameUser()->rewardInfo.clear(); //reward 奖赏
GameUser::GetGameUser()->rewardInfo.swap(GameUser::GetGameUser()->rewardInfo);
//////////////////////
,clear()可以清空所有元素。但是即使clear(),所占用的内存空间依然如故。如果你需要空间动态缩小,可以考虑使用deque。如果非要用vector,这里有一个办法:
在《effective STL》和其实很多C++文章中都有指明,用clear()无法保证内存回收。但是swap技法可以。具体方法如下所示:
vector<int> ivec;
ivec.push_back(1);ivec.push_back(1);ivec.push_back(2);ivec.push_back(2);
vector<int>().swap(ivec); //或者ivec.swap(vector<int>());
vector<int>().swap(ivec); 或者如下所示 加一对大括号都可以,意思一样的:
{
std::vector<int> tmp;
ivec.swap(tmp);
}
加一对大括号是可以让tmp退出{}的时候自动析构
refrashCycleSign();
}
else
{
MessageBoxLayer::getMessageBoxPoint()->callMessageBoxRemove();
// MessageBoxLayer::getMessageBoxPoint()->createWithTip("提示", "签到失败!", ONE_BUTTON);
MessageBoxLayer::getMessageBoxPoint()->createWithTip("错误", GameUtil::getErrorString(ret->result.nRet).c_str(), ONE_BUTTON);
}
m_selectDay = 0;
m_isPressButton = false;
delete ret;
}