尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17323935
<捕鱼达人>回顾
【cocos2d-x IOS游戏开发-捕鱼达人1】内容介绍
上节回顾
【cocos2d-x IOS游戏开发-城市跑酷9】没有各种坑还叫游戏吗
定义一个地形类,继承于CCSprite,可以想象着把一块块街区与坑看做是一个个精灵。
//地形类 class Terrain : public CCSprite { CCArray * _blockPool; int _blockPoolIndex; CCArray * _blocks; int _lastBlockHeight; int _lastBlockWidth; int _minTerrainWidth; bool _showGap; CCSize _screenSize; int _currentPatternIndex; int _currentPatternCnt; int _currentWidthIndex; int _currentHeightIndex; int _currentTypeIndex; int _increaseGapInterval; float _increaseGapTimer; int _gapSize; void initTerrain (void); void addBlocks(int currentWidth); void distributeBlocks(); void initBlock(Block * block); inline float getWidth () { int count = _blocks->count(); int width = 0; Block * block; for (int i = 0; i < count; i++) { block = (Block *) _blocks->objectAtIndex(i); width += block->getWidth(); } return width; } public: Terrain(void); ~Terrain(void); CC_SYNTHESIZE(bool, _startTerrain, StartTerrain); static Terrain * create(); void activateChimneysAt (Player * player); void checkCollision (Player * player); void move (float xMove); void reset (void); };
定义vector来随机生成街区的模式、高、宽以及类型:
int patterns[] = {1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,3,3,3}; int widths[] = {2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4}; int heights[] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,3,3,3,3,3,3,4}; int types[] = {1,2,3,4,1,3,2,4,3,2,1,4,2,3,1,4,2,3,1,2,3,2,3,4,1,2,4,3,1,3,1,4,2,4,2,1,2,3}; vector<int> _blockPattern (patterns, patterns + sizeof(patterns) / sizeof(int)); vector<int> _blockWidths (widths, widths + sizeof(widths) / sizeof(int)); vector<int> _blockHeights (heights, heights + sizeof(heights) / sizeof(int)); vector<int> _blockTypes (types, types + sizeof(types) / sizeof(int));
先介绍一种随机算法:
void Terrain::initTerrain () { _increaseGapInterval = 5000; _increaseGapTimer = 0; _gapSize = 2; _blockPool = CCArray::createWithCapacity(20); _blockPool->retain(); //init object pools Block * block; for (int i = 0; i < 20; i++) { block = Block::create(); this->addChild(block); _blockPool->addObject(block); } _blocks = CCArray::createWithCapacity(20); _blocks->retain(); _minTerrainWidth = _screenSize.width * 1.5f; random_shuffle(_blockPattern.begin(), _blockPattern.end()); random_shuffle(_blockWidths.begin(), _blockWidths.end()); random_shuffle(_blockHeights.begin(), _blockHeights.end()); this->addBlocks(0); }地形图移动:
void Terrain::move (float xMove) { if (xMove < 0) return; if (_startTerrain) { if (xMove > 0 && _gapSize < 5) _increaseGapTimer += xMove; if (_increaseGapTimer > _increaseGapInterval) { _increaseGapTimer = 0; _gapSize += 1; } } this->setPositionX(this->getPositionX() - xMove); Block * block; block = (Block *) _blocks->objectAtIndex(0); if (this->getPositionX() + block->getWidth() < 0) { _blocks->removeObjectAtIndex(0); _blocks->addObject(block); this->setPositionX(this->getPositionX() + block->getWidth()); float width_cnt = this->getWidth() - block->getWidth() - ((Block *) _blocks->objectAtIndex(0))->getWidth(); this->initBlock(block); this->addBlocks(width_cnt); } }