前言
前一篇博文讲述了项目的架构,从中也可以看出GameTool 的主要功能是显示并随时更新分数和最高分数,其中主要用到的是Label
设计
GameTool主要包括三个Label,用于显示标题的2048+,显示分数的Score, 最高分的Best;其中标题与另外两个区别在于没有背景,且内容不可更改(本项目设置为不可改)
1. 创建标签
对于这三个标签而言,创建流程相差无机,具体代码如下:
auto layerIcon = LayerColor::create(Color4B(230,230,0),100,60); auto label = Label::createWithSystemFont("2048+","Arial",36); //auto label = Label::createWithTTF("小兵传奇","ClearSansBold.ttf",36); label->setTextColor(Color4B(134,134,255)); label->setPosition(60,30); layerIcon->addChild(label); this->addChild(layerIcon);
代码分析:
背景创建: LayerColor::create(Color4B(230,255),60);
表示生成一个色块,大小为(100,60),颜色为(230,230,0,255)最后一个为透明度(255不透明),LayerColor继承Layer而来,本处使用主要是用于创建label的背景
标签创建: Label::createWithSystemFont(“2048”,“Arial”,36);
创建标签,用于显示内容,上面表示的是用默认字体创建,也可以利用其他的方式如 createWithTTF,createWithBMFont等,利用vs时会有相关提示(直白来说就是看你选择的是什么字体,fnt? ttf? )
创建Label之后,可以设置相关属性,这里主要提及的是设置字体颜色, setTextColor(Color4B(…)),setString(…),setPosition(Vec2(60,30)); 对于设置位置需要记住Label的锚点是在正中心,因此其坐标不应该设置为父节点的(0,0)处
添加子节点: addChild(node)
2. 分数更新
这里添加了两个变量_score,_bestScoer, 分别保存当前的分数和最高分,当滑动合并方块时,需要更新分数,当当前分数大于最高分时需要更新最高分
int GameTool::getScore() { return _score; } void GameTool::setScore(int score) { _score = score; scoreLabel->setString(Value(_score).asString()); } int GameTool::getBestScore() { return _bestScore; } void GameTool::setBestScore(int bestScore) { _bestScore = bestScore; bestLabel->setString(Value(_bestScore).asString()); } void GameTool::updateScore(int addScore) { setScore(_score + addScore); updateBestScore(); // update the bset score if necessary } void GameTool::resetScore() { setScore(0); } void GameTool::updateBestScore() { if(_score < _bestScore) return ; setBestScore(_score); }
上面的代码很容易理解, 这里额外提一下Cocos2dx中声明了一个宏 CC_SYINTHESIZE, 用于集成类变量的声明,并自动实现getXXX()和setXXX(…),本处没有使用是因为在setScore函数中需要对_socreLabel的内容值同步进行更新
3. 类型转换
上面的代码
bestLabel->setString(Value(_bestScore).asString());中涉及到了类型转换,将int转换为std::string 类型
Value是cocos2dx-3.x版本新引入的容器,可以实现基本类型的转换,其使用规则基本如上所示, 创建一个Value()对象,并调用asXXX()转换为相应的数据类型即可
实现
本类的设计非常简单,上面基本列出所有的功能点,下面贴出本类的代码以供参考:
#pragma once #include "cocos2d.h" USING_NS_CC; class GameTool : public cocos2d::Layer { public: static GameTool* getInstance(); virtual bool init(); virtual bool initScore(); void loadScore(int type); void updateScore(int addScore); void resetScore(); void setScore(int score); int getScore(); void setBestScore(int bestScore); int getBestScore(); private: CREATE_FUNC(GameTool); void updateBestScore(); Label* scoreLabel; Label* bestLabel; int _score; int _bestScore; static GameTool* _instance; };
#include "GameTool.h" #include "DataConf.h" GameTool* GameTool::_instance = nullptr; GameTool* GameTool::getInstance() {//采用单例模式创建对象 if(_instance==nullptr) _instance = create(); return _instance; } bool GameTool::init() { do{ CC_BREAK_IF(!Layer::init()); // set tool layer's size and position this->setContentSize(Size(300,60)); this->setPosition(10,410); auto layerIcon = LayerColor::create(Color4B(230,60); auto label = Label::createWithSystemFont("2048+",36); //auto label = Label::createWithTTF("小兵传奇",36); label->setTextColor(Color4B(134,255)); label->setPosition(60,30); layerIcon->addChild(label); this->addChild(layerIcon); initScore(); }while(0); return true; } bool GameTool::initScore() { auto scoreIcon = LayerColor::create(Color4B(186,172,159,80,50); auto scoreTitleLabel = Label::createWithSystemFont("SCORE",16); scoreTitleLabel->setPosition(40,35); scoreIcon->setPosition(Vec2(130,5)); scoreIcon->addChild(scoreTitleLabel); scoreLabel = Label::createWithSystemFont(Value(_score).asString(),20); scoreLabel->setPosition(40,10); scoreIcon->addChild(scoreLabel); this->addChild(scoreIcon); auto bestIcon = LayerColor::create(Color4B(186,50); auto bestTitleLabel = Label::createWithSystemFont("BEST",16); bestTitleLabel->setPosition(40,35); bestIcon->setPosition(Vec2(220,5)); bestIcon->addChild(bestTitleLabel); bestLabel = Label::createWithSystemFont(Value(_bestScore).asString(),20); bestLabel->setPosition(40,10); bestIcon->addChild(bestLabel); this->addChild(bestIcon); // 首次从文件中读取最高分 loadScore(UserDefault::getInstance()->getIntegerForKey("type",1)); return true; } // 从记录中获取当前分数和最高分 void GameTool::loadScore(int type) { setScore(UserDefault::getInstance()->getIntegerForKey(Value(type).asString().append("score").c_str(),0)); setBestScore(UserDefault::getInstance()->getIntegerForKey(Value(type).asString().append("best_score").c_str(),0)); } int GameTool::getScore() { return _score; } void GameTool::setScore(int score) { _score = score; scoreLabel->setString(Value(_score).asString()); } int GameTool::getBestScore() { return _bestScore; } void GameTool::setBestScore(int bestScore) { _bestScore = bestScore; bestLabel->setString(Value(_bestScore).asString()); } void GameTool::updateScore(int addScore) { setScore(_score + addScore); updateBestScore(); // update the bset score if necessary } void GameTool::resetScore() { setScore(0); } void GameTool::updateBestScore() { if(_score < _bestScore) return ; setBestScore(_score); }