前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cocos2dx-v3.5 2048 (二): GameTool的设计与实现

cocos2dx-v3.5 2048 (二): GameTool的设计与实现

作者头像
一灰灰blog
发布2018-02-06 12:07:29
6640
发布2018-02-06 12:07:29
举报
文章被收录于专栏:小灰灰小灰灰

前言


前一篇博文讲述了项目的架构,从中也可以看出GameTool 的主要功能是显示并随时更新分数和最高分数,其中主要用到的是Label

image
image

设计


GameTool主要包括三个Label,用于显示标题的2048+, 显示分数的Score, 最高分的Best;其中标题与另外两个区别在于没有背景,且内容不可更改(本项目设置为不可改)

1. 创建标签

对于这三个标签而言,创建流程相差无机,具体代码如下:

代码语言:javascript
复制
auto layerIcon = LayerColor::create(Color4B(230, 230, 0, 0), 100, 60);
auto label = Label::createWithSystemFont("2048+", "Arial", 36);
//auto label = Label::createWithTTF("小兵传奇", "ClearSansBold.ttf", 36);
label->setTextColor(Color4B(134,134,134, 255));
label->setPosition(60, 30);
layerIcon->addChild(label);
this->addChild(layerIcon);

代码分析:

背景创建: LayerColor::create(Color4B(230, 230, 0, 255), 100, 60);

表示生成一个色块,大小为(100,60),颜色为(230,230,0,255)最后一个为透明度(255不透明),LayerColor继承Layer而来,本处使用主要是用于创建label的背景

标签创建: Label::createWithSystemFont(“2048”, “Arial”, 36);

创建标签,用于显示内容,上面表示的是用默认字体创建,也可以利用其他的方式如 createWithTTF, createWithBMFont等,利用vs时会有相关提示(直白来说就是看你选择的是什么字体,fnt? ttf? )

image
image

创建Label之后,可以设置相关属性,这里主要提及的是设置字体颜色, setTextColor(Color4B(…)), setString(…),  setPosition(Vec2(60, 30)); 对于设置位置需要记住Label的锚点是在正中心,因此其坐标不应该设置为父节点的(0,0)处

添加子节点: addChild(node)

2. 分数更新

这里添加了两个变量_score, _bestScoer, 分别保存当前的分数和最高分,当滑动合并方块时,需要更新分数,当当前分数大于最高分时需要更新最高分

代码语言:javascript
复制
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. 类型转换

上面的代码

代码语言:javascript
复制
bestLabel->setString(Value(_bestScore).asString());

中涉及到了类型转换,将int转换为std::string 类型

Value是cocos2dx-3.x版本新引入的容器,可以实现基本类型的转换,其使用规则基本如上所示, 创建一个Value()对象,并调用asXXX()转换为相应的数据类型即可

实现


本类的设计非常简单,上面基本列出所有的功能点,下面贴出本类的代码以供参考:

代码语言:javascript
复制
#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;
};
代码语言:javascript
复制
#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, 230, 0, 0), 100, 60);
		auto label = Label::createWithSystemFont("2048+", "Arial", 36);
		//auto label = Label::createWithTTF("小兵传奇", "ClearSansBold.ttf", 36);
		label->setTextColor(Color4B(134,134,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, 255), 80, 50);
	auto scoreTitleLabel = Label::createWithSystemFont("SCORE", "Arial", 16);
	scoreTitleLabel->setPosition(40,35);
	scoreIcon->setPosition(Vec2(130, 5));
	scoreIcon->addChild(scoreTitleLabel);

	scoreLabel = Label::createWithSystemFont(Value(_score).asString(), "Arial", 20);
	scoreLabel->setPosition(40, 10);
	scoreIcon->addChild(scoreLabel);
	this->addChild(scoreIcon);


	auto bestIcon = LayerColor::create(Color4B(186, 172, 159, 255), 80, 50);
	auto bestTitleLabel = Label::createWithSystemFont("BEST", "Arial", 16);
	bestTitleLabel->setPosition(40, 35);
	bestIcon->setPosition(Vec2(220, 5));
	bestIcon->addChild(bestTitleLabel);

	bestLabel = Label::createWithSystemFont(Value(_bestScore).asString(), "Arial", 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);
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 设计
  • 实现
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档