cocos2d-x 3.2 它 2048 —— 第三
***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************
OK。抓紧更新吧。
长话短说,直奔主题。第三篇:
——数字块类的创建
数字块是神马?
——就是那个,你滑动的数字,还有随机出现的数字。
我们,先创建一个类NumberTiled,继承自Node:
NumberTiled.h:
#ifndef __test2048_NumberTiled_H__
#define __test2048_NumberTiled_H__
#include "cocos2d.h"
USING_NS_CC;
class NumberTiled : public Node
{
public:
	// 存储行列位置 及 该位置的数字值
	int m_row,m_col;
	int m_number;
	// 移动到r,c 这个位置
	void moveTo( int r , int c );
	CREATE_FUNC(NumberTiled);
	bool init();
};
#endif
NumberTiled.cpp:
#include "NumberTiled.h"
#include "GameDefine.h"
USING_NS_CC;
bool NumberTiled::init()
{
	if( !Node::init() )
	{
		return false;
	}
	// 背景层
	auto bk = LayerColor::create( Color4B(200,200,200,255),GAME_TILED_WIDTH,GAME_TILED_HEIGHT );
	this->addChild(bk);
	
	// 数字层——随机出一个数字。若数字等于7 则产生4否则产生2
	int n = rand()%10;
	this -> m_number = n==7?4:2;
	// 依据数字的值,赋予不同颜色
	switch ( this -> m_number )
	{
	case 2:	bk -> setColor(Color3B(230,220,210));	break;
	case 4:	bk -> setColor(Color3B(230,210,190));	break;
	default:	break;
	}
	// 创建字体,并将本块的数字显现上去
	TTFConfig config("HelloKitty.ttf",40);
	auto label = Label::createWithTTF(config, StringUtils::format("%d",this->m_number));  
	label -> setPosition(Point(GAME_TILED_WIDTH/2,GAME_TILED_HEIGHT/2));
	label -> setColor(Color3B::BLACK);
	bk -> addChild( label );
	return true;
}
void NumberTiled::moveTo( int r , int c )
{
	this -> m_row = r;
	this -> m_col = c;
	this -> setPosition( 
		Point( m_col * GAME_TILED_WIDTH + GAME_TILED_BOARD_WIDTH * (m_col + 1),
		m_row * GAME_TILED_HEIGHT + GAME_TILED_BOARD_WIDTH * (m_row+1) 
		));
}好的,这个类基础的功能完毕了,
就是初始化 和 移动(出现) 都某个位置。
接下来,要在我们的界面上随机展现出来呀~
转到游戏界面,
上一篇文章中,我们加入了 逻辑数组map ,
如今,我们还要加入一个Vector(集合)来保存全部的块。
GameScene.h:
Vector<NumberTiled *> m_allTiled;
那就要随机产生一个块咯,
加入一个方法——newNumberTiled
这个函数作用就是,产生一个新块:
void GameScene::newNumberTiled()
{
	// 创建一个 数字块的实例
	auto tiled = NumberTiled::create();
	// 找到有几个空暇的位置
	int freeCount = 16 - m_allTiled.size();
	int num = rand() % freeCount;
	int row = 0,col = 0,count = 0;
	bool find = false;
	// 产生数字,一定在空白区域
	for( ; row < GAME_ROWS ; ++row )
	{
		for( col = 0 ; col < GAME_COLS ; ++col )
		{
			if( map[row][col] == 0 )
			{
				// 记录空白区域的数量
				++count;
				if( count >= num )
				{
					find = true;
					break;
				}
			}
		}
		if( find )
		{
			break;
		}
	}
	// 注意在colorBack中加入tiled哟
	colorBack -> addChild( tiled );
	tiled -> moveTo ( row , col );
	m_allTiled.pushBack(tiled);
	map[ row ][ col ] = m_allTiled.getIndex(tiled)+1;
}恩,详细解释。在代码中都已给出了。
说明一下怎么在空的位置随机产生块:
首先。获取有多少空位置,
然后。随机产生一个数。小于空位置总数
接着设定count为0,
从第一个位置到最后一个位置遍历,
遇到空位置,count+1。
若大于产生的随机数,就在该位置放置数字块。
如今,能够执行一下。看一看效果了:
Ok,这次就到这里。
下次内容就是。对触摸屏幕的处理~
敬请期待哟~
本篇文章代码:http://pan.baidu.com/s/1sjHLNp7
***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************
版权声明:本文博客原创文章。博客,未经同意,不得转载。
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号