您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x 3.2 之 别踩白块(第三篇)

2015-07-01 15:56 465 查看
***************************************转载请注明出处: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;
}

原理,很简单,就是新建一个层,把触摸事件吞掉。

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******************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息