***************************************转载请注明出处:http://blog.csdn.net/lttree@H_502_2@******************************************@H_502_2@
@H_502_2@
@H_502_2@
OK,抓紧更新吧。@H_502_2@
——就是那个,你滑动的数字,还有随机出现的数字。@H_502_2@@H_502_2@
我们,先创建一个类NumberTiled,继承自Node:@H_502_2@@H_502_2@
NumberTiled.h:@H_502_2@@H_502_2@
#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:@H_502_2@@H_502_2@
#include "NumberTiled.h" #include "GameDefine.h" USING_NS_CC; bool NumberTiled::init() { if( !Node::init() ) { return false; } // 背景层 auto bk = LayerColor::create( Color4B(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) )); }
好的,这个类基础的功能完成了,
就是初始化 和 移动(出现) 都某个位置。@H_502_2@@H_502_2@
接下来,要在我们的界面上随机展现出来呀~@H_502_2@@H_502_2@
上一篇文章中,我们添加了 逻辑数组map ,@H_502_2@@H_502_2@
现在,我们还要添加一个Vector(集合)来保存所有的块,@H_502_2@@H_502_2@
GameScene.h:@H_502_2@@H_502_2@
Vector<NumberTiled *> m_allTiled;然后,在init中,我们创建了出来了格子,接下来,有了数字块以后,
那就要随机产生一个块咯,@H_502_2@@H_502_2@
添加一个方法——newNumberTiled@H_502_2@@H_502_2@
这个函数作用就是,产生一个新块:@H_502_2@@H_502_2@
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; }
恩,具体解释,在代码中都已给出了。
首先,获取有多少空位置,@H_502_2@@H_502_2@
然后,随机产生一个数,小于空位置总数@H_502_2@@H_502_2@
接着设定count为0,@H_502_2@@H_502_2@
从第一个位置到最后一个位置遍历,@H_502_2@@H_502_2@
遇到空位置,count+1,@H_502_2@@H_502_2@
若大于产生的随机数,就在该位置放置数字块。@H_502_2@@H_502_2@
现在,可以运行一下,看一看效果了:@H_502_2@@H_502_2@
@H_502_2@
Ok,这次就到这里,@H_502_2@
敬请期待哟~@H_502_2@
@H_502_2@
@H_502_2@
本篇文章代码:http://pan.baidu.com/s/1sjHLNp7@H_502_2@
@H_502_2@
@H_502_2@
@H_502_2@
***************************************转载请注明出处:http://blog.csdn.net/lttree@H_502_2@******************************************@H_502_2@