zoukankan      html  css  js  c++  java
  • Cocos2d-x 3.2 之 别踩白块(第三篇)

    ***************************************转载请注明出处: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******************************************

  • 相关阅读:
    Quartus 16.1 signaltap问题
    黑金AX301开发板视频图像处理:探索
    YOLO算法学习
    Ubuntu 16.04运行altera opencl sdk(AOCL)
    Ubuntu下quartus发现不到usb blaster的问题
    Ubuntu 16.04安装altera opencl sdk(AOCL)
    ZYNQ术语及缩写
    BinaryNet: Training Deep Neural Networks with Weights and ActivationsConstrained to +1 or −1
    卷积神经网络优化方法
    FINN: A Framework for Fast, Scalable Binarized Neural Network Inference_2016_CSCV
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6820313.html
Copyright © 2011-2022 走看看