cocos2dx 学习笔记(4) --《万圣节大作战》项目分析(2)
今天,看完了项目完全代码:并分析一下优劣。
进入游戏主题,然后,由于是三消游戏,我们主要精力放在OnTouchMoved函数上,这是手指滑动之后,做出的动作,之后的消除,消除之后,掉落等动画都是由这个函数触发的。
这里,作者是利用滑动过程中,手指的点,是否移动上下左右方框中,这里只出现上面的代码:
auto upRect = Rect(staSprite->getPositionX() - halfSpriteWidth, staSprite->getPositionY() + halfSpriteHeight, SPRITE_WIDTH, SPRITE_WIDTH); if (upRect.containsPoint(location)) { ++row; if ( row < ROWS ) { endSprite = map[row][col]; } fourBoom = true; swapSprite(); return; }
上面主要看 upRect.containsPoint(location) ,记住Rect的几个函数即可,ios里面也有类似的函数CGRectCotainsPoint
swapSprite()函数做出了交换方块,然后:
void GameScene::swapSprite() { isAction = true; //开始动作 isTouchEna = false; //不允许再次滑动 if (!staSprite || !endSprite) {//如果为空 fourBoom = true; return; } Point posOfSrc = staSprite->getPosition();//中心点 Point posOfDest = endSprite->getPosition(); float time = 0.2;//动画事件 map[ staSprite -> getRow() ][staSprite -> getCol() ] = endSprite; //内存地址的交换 map[ endSprite -> getRow() ][endSprite -> getCol() ] = staSprite; int tmpRow = staSprite->getRow(); int tmpCol = staSprite->getCol(); staSprite->setRow(endSprite->getRow()); staSprite->setCol(endSprite->getCol()); endSprite->setRow(tmpRow); endSprite->setCol(tmpCol); std::list<SpriteShape *> colChainListOfFirst; getColChain(staSprite, colChainListOfFirst); std::list<SpriteShape *> rowChainListOfFirst; getRowChain(staSprite, rowChainListOfFirst); std::list<SpriteShape *> colChainListOfSecond; getColChain(endSprite, colChainListOfSecond); std::list<SpriteShape *> rowChainListOfSecond; getRowChain(endSprite, rowChainListOfSecond); //判断是否可以三消 if (colChainListOfFirst.size() >= 3 || rowChainListOfFirst.size() >= 3 || colChainListOfSecond.size() >= 3 || rowChainListOfSecond.size() >= 3) { //可以三消就进行交换动画 staSprite->runAction(MoveTo::create(time, posOfDest)); endSprite->runAction(MoveTo::create(time, posOfSrc)); return; } //不能三消就会去,并且报错 map[ staSprite -> getRow()][staSprite -> getCol() ] = endSprite; map[ endSprite -> getRow()][endSprite -> getCol() ] = staSprite; tmpRow = staSprite->getRow(); tmpCol = staSprite->getCol(); staSprite->setRow(endSprite->getRow()); staSprite->setCol(endSprite->getCol()); endSprite->setRow(tmpRow); endSprite->setCol(tmpCol); staSprite->runAction(Sequence::create( MoveTo::create(time, posOfDest), MoveTo::create(time, posOfSrc), NULL)); endSprite->runAction(Sequence::create( MoveTo::create(time, posOfSrc), MoveTo::create(time, posOfDest), NULL)); }
该游戏就讲解到此。
欢迎一起来研究,QQ 253807688