***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************

别踩白块。第三篇。。

距离第二篇都快过去半年了。。

一直没抽空来完好它,

这次简单的完好一下:

> 触摸屏蔽

> 最高分的存储

> 时间显示优化

> 初始化优化

主要就是这几方面的优化了,其它杂七杂八的。就没有列出了,能够參考源代码

1. 触摸屏蔽

每次踩到白块,假设不屏蔽触摸,就会发生非常糟糕的东东(能够继续玩下去。直到跳到下一个场景)。

所以,我们须要实现 触摸的屏蔽。

这里,我直接新建了一个继承自Layer的类,然后在该加的地方 create 然后 addChild即可了。

非常简洁,重用性也高:

// SwallowLayer.h  

#include "cocos2d.h"

USING_NS_CC;

class SwallowLayer : public Layer
{
public:
/***** 初始化函数 *****/
virtual bool init();
CREATE_FUNC(SwallowLayer); };
// SwallowLayer.cpp

#include "SwallowLayer.h"

bool SwallowLayer::init( )
{
if( !Layer::init() ) {
return false;
} // 加入监听器
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan= [this](Touch* t,Event* e){
CCLOG("touch swallow layer");
return true;
};
listener->setSwallowTouches(true);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); return true;
}

原理。非常easy,就是新建一个层,把触摸事件吞掉。

2.最高分的存储

这个。说过非常多遍了,主要是想讲一下。用一个结束界面。再玩不同模式时,显示不同模式的数值,

比方。我这个游戏有两个模式:固定时间  和  固定行数。

每次游戏结束,都要向游戏结束界面层传递些数据,

比方,玩家是否完毕游戏,假设是 固定时间 模式,完毕后,走了多少行。假设是 固定行数 模式,花了多少时间?

我的方法,就是在游戏结束层,进行函数的重载:

//接受模式(Limit Block OR Limit Time ),// true 为LimitBlocks。false为LimitTime
void createText( bool mode , double num );
void createText( bool mode );

第一个函数,事实上表示的是。玩家完毕游戏,传递 模式 和 分数(时间、行数),

第二个函数,表示的是。游戏失败(踩到白块啦~),所以,直接传递模式(用来显示。最高分)。

void GameOver::createText( bool mode , double num )
{
if( mode ) {
// 获取最高分
double highest = UserDefault::getInstance()->getDoubleForKey("HIGHESTTIME",0); CCLOG("highest time %f",highest); auto _highest = Label::create(StringUtils::format("Highest: %.2f",highest),"fonts/Marker Felt.ttf",34);
_highest->setTextColor(Color4B::RED);
_highest->setPosition(visibleSize.width/2+100,visibleSize.height/2+100);
this->addChild(_highest); auto _score = Label::create(StringUtils::format("Yours: %.2f",num),"fonts/Marker Felt.ttf",60);
_score->setTextColor(Color4B::BLUE);
_score->setPosition(visibleSize.width/2,visibleSize.height/2-50);
this->addChild(_score); if( highest > num || highest == 0 ) {
CCLOG("this time %f",num);
UserDefault::getInstance()->setDoubleForKey("HIGHESTTIME",num);
CCLOG("this highest time %f",UserDefault::getInstance()->getDoubleForKey("HIGHESTTIME",num));
}
}
else
{
// 获取最高分
int highest = UserDefault::getInstance()->getIntegerForKey("HIGHESTBLOCKS",0); auto _highest = Label::create(StringUtils::format("Highest: %d",highest),"fonts/Marker Felt.ttf",34);
_highest->setTextColor(Color4B::RED);
_highest->setPosition(visibleSize.width/2+100,visibleSize.height/2+100);
this->addChild(_highest); auto _score = Label::create(StringUtils::format("Yours: %d",(int)num),"fonts/Marker Felt.ttf",60);
_score->setTextColor(Color4B::BLUE);
_score->setPosition(visibleSize.width/2,visibleSize.height/2-50);
this->addChild(_score); if( highest < num ) {
UserDefault::getInstance()->setIntegerForKey("HIGHESTBLOCKS",(int)num);
}
}
} void GameOver::createText( bool mode )
{
// mode——true:Limit Block,false:Limit Time
if( mode ) {
// 获取最高分
double highest = UserDefault::getInstance()->getDoubleForKey("HIGHESTTIME",0); auto _highest = Label::create(StringUtils::format("Highest: %.2f",highest),"fonts/Marker Felt.ttf",34);
_highest->setTextColor(Color4B::RED);
_highest->setPosition(visibleSize.width/2+100,visibleSize.height/2+100);
this->addChild(_highest);
}
else
{
// 获取最高分
long highest = UserDefault::getInstance()->getIntegerForKey("HIGHESTBLOCKS",0); auto _highest = Label::create(StringUtils::format("Highest: %d",highest),"fonts/Marker Felt.ttf",34);
_highest->setTextColor(Color4B::RED);
_highest->setPosition(visibleSize.width/2+100,visibleSize.height/2+100);
this->addChild(_highest);
} auto _label = Label::create("Fail!","fonts/Marker Felt.ttf",60);
_label->setTextColor(Color4B::RED);
_label->setPosition(visibleSize.width/2,visibleSize.height/2-50);
this->addChild(_label); }



3.时间显示的优化

之前做的时间显示,晃得眼睛疼。

并且,后来调试的时候,发现可能乱码。

所以,获取系统时间函数改了一个:

double modeLimitTime::getMillSecond()
{
struct timeval tv;
gettimeofday(&tv, nullptr); CCLOG("CurrentTime MillSecond %f", (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000); return (double)tv.tv_sec * 1000 + (double)tv.tv_usec / 1000;
}

输出的时候,仅仅输出小数点后两位:

//时间的偏差
_time = (getMillSecond()-startTime)/1000.0;
CCLOG("SubTime MillSecond %f", _time);
//强转offset为double类型
timerLabel->setString(StringUtils::format("%.2f",_time));

4.初始化优化

这个是我后来执行的时候,发现,每次都有些数据滞留,并没有清空。

找到最后,发现是Block的Vector没有清空,

所以在游戏结束的时候。一定不要忘了将游戏数据清空:

// 将Vector清空,避免影响后面游戏
auto bs = Block::getBlocks();
bs->clear();

Ok。临时就是这些了,

接下来的时间,我要准备准备考试什么的了。。

源代码:  >
这里 <

APK: >
这里 <

***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************

最新文章

  1. Android Weekly Notes Issue #218
  2. Volley的基本使用(转)
  3. BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
  4. 基于visual Studio2013解决C语言竞赛题之0303最大数
  5. ACM-简单的主题Ignatius and the Princess II——hdu1027
  6. tag标签记录
  7. Oracle约束、索引
  8. vBox Arch UEFI LVM安装
  9. python第四天,list补充
  10. day9 集合基础命令
  11. 微信小程序开发——活动规则类文案文件读取及自动转换为小程序排版代码
  12. word中公式居中编号在最右端
  13. 【Java nio】java nio笔记
  14. 4 使用Selenium模拟登录csdn,取出cookie信息,再用requests.session访问个人中心(保持登录状态)
  15. 20155231 实验四 Android程序设计
  16. 20145216史婧瑶《Java程序设计》第2周学习总结
  17. HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)
  18. MyEclipse迁移过程中Tomcat版本不一致的解决办法
  19. JUNIT的用法简要总结
  20. gearman安装实录

热门文章

  1. What is the purpose of mock objects?
  2. 利用【深度网络】高效提取feature
  3. perl一次读取多行文本的策略
  4. linux 远程同步数据工具rsync (2)
  5. C++中virtual(虚函数)的用法
  6. 利用Python,四步掌握机器学习
  7. cbuffer padding
  8. http://blog.csdn.net/muzizongheng/article/details/46795243
  9. 2017.9.15 postgresql批量插入造成冲突后执行更新
  10. 人工智能真NB?何不去炒股?