尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17162501
上节回顾
【cocos2d-x IOS游戏开发-捕鱼达人17】炮弹攻击判定
1、积分板效果
积分的获取
不同的鱼被捕后会得到不同的积分
积分板效果
鱼被捕时,会在鱼的相应位置显示得分
记分板更新
积分板资源
2、积分保存
采用cocos2d-x内置的CCUserDefault
保存
cocos2d::CCUserDefault::sharedUserDefault()->setIntegerForKey("mark",1234);
cocos2d::CCUserDefault::sharedUserDefault()->flush();
读取
int i = cocos2d::CCUserDefault::sharedUserDefault()->getIntegerForKey("mark");
开始游戏后获取积分:
int mark = cocos2d::CCUserDefault::sharedUserDefault()->getIntegerForKey("mark",-1); if(mark==-1){ m_iTotalMark = 100; } else{ m_iTotalMark = mark; }添加积分板:
m_PlayLayout.addChild(&m_MarkLayout); m_MarkLayout.setPositionX(20); m_MarkLayout.setPositionY(-1);改变积分:
void slot_EnterGame() { m_PlayLayout.setVisible(true); m_MarkLayout.changeNum(m_pGameLogic->getMark()); }积分改变实现:
#ifndef __UI_MARK_LAYOUT_H__ #define __UI_MARK_LAYOUT_H__ #include "cocos2d.h" #include "../include/SignalSlot.h" USING_NS_CC; namespace fishingjoy { namespace ui { class UI_MarkLayout:public CCLayer { public: CCSprite* numbers[5]; public: UI_MarkLayout() { for(int i = 0; i < 5; ++i){ CCSprite* n = CCSprite::create("ui/N0.png"); n->setAnchorPoint(CCPointZero); addChild(n); n->setPositionX(i*25.5); n->setPositionY(0); numbers[i] = n; } } void changeNum(int num) { if(num > 99999){ num = 99999; } if(num < 0){ num = 0; } int g = num%10; changeNum(4,g); int s = (num/10)%10; changeNum(3,s); int b = (num/100)%10; changeNum(2,b); int q = (num/1000)%10; changeNum(1,q); int w = (num/10000)%10; changeNum(0,w); } private: void changeNum(int idx,int num) { CCSprite* p = numbers[idx]; char buf[32]; sprintf(buf,"ui/N%d.png",num); CCTexture2D* pTex = CCTextureCache::sharedTextureCache()->addImage(buf); p->setTexture(pTex); } }; } } #endif
本节源代码与工程文件:【资源引用页】
大家还可以实现鱼被捕后的分数显示效果。