转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/24297597
源代码下载:http://download.csdn.net/detail/oyangyufu/7272177
四、触摸处理ccTouchEnded()流程
1、获取触摸屏幕的坐标转换成地图坐标
2、推断该是否在有效范围、是否为空
3、播放触摸声音
4、依据点击坐标获取精灵、并放大1.3倍
5、依据坐标获取前一个点击的精灵
6、比較这两精灵能否够连接消除
是。播放消除声音,清除这两坐标node。隐藏这两精灵
否,缩小前一个精灵,并有抖动动画
7、把当前点击坐标赋值于前一个坐标. (前一坐标初始值(-1, -1))
//触摸结束 void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView()); //获取触摸屏幕的坐标 location = this->convertToNodeSpace(location); //location=CCPointMake(location.x, location.y-40); location=this->pointOfView(location); CCLOG("x:%f",location.x); CCLOG("y:%f",location.y); if (this->isValiableNode(location)==false) { return; } if (this->isEmptyNode(location)) { return; } SimpleAudioEngine::sharedEngine()->playEffect("12.wav"); //触摸声音 if (this->isSamePoints(location, prePoint)) { return; } //点击当前精灵 CCSprite *spritecurrent=(CCSprite *)this->getChildByTag(TAG_START_SPRITE+this->indexFromPoint(location)); spritecurrent->setScale(1.3); //放大 CCLOG("%d",this->indexFromPoint(location)); if (this->isValiableNode(prePoint)) { //前一个精灵 CCSprite *spritepre=(CCSprite *)this->getChildByTag(TAG_START_SPRITE+this->indexFromPoint(prePoint)); if (this->canClearTwo(prePoint, location)) //比較这两精灵能否够连接消除 { SimpleAudioEngine::sharedEngine()->playEffect("4.wav"); this->clearNode(location); this->clearNode(prePoint); spritecurrent->setVisible(false); spritepre->setVisible(false); } else { spritepre->setScale(0.9); this->scaleAnimation(spritepre); } } prePoint=location; //把当前点击坐标赋值于前一个坐标 }