在游戏设计中,经常会涉及到用手指控制游戏主角的移动,下面就是其cocos2dx的实现(基于cocos2dx2.1.4)
<span >bool MainScene::cctouchBegan(CCtouch *ptouch,CCEvent *pEvent){ CCPoint point = ptouch->getLocationInVIEw(); point = CCDirector::sharedDirector()->convertToGL(point); prevIoUsPoint = CCPointMake(point.x,point.y); return true;}voID MainScene::cctouchmoved(CCtouch *ptouch,CCEvent *pEvent){ //static CCPoint prevIoUsPoint; CCPoint curpos = ptouch->getLocationInVIEw(); curpos = CCDirector::sharedDirector()->convertToGL(curpos); //重新添加触摸移动代码 CCPoint mPoint = m_player->getposition(); CCPoint distaceXY = CCPointMake(mPoint.x-curpos.x,mPoint.y-curpos.y); float xx = curpos.x-prevIoUsPoint.x; float yy = curpos.y-prevIoUsPoint.y; prevIoUsPoint = CCPointMake(curpos.x,curpos.y); if(mPoint.x+xx >= 770 || mPoint.x+xx <= 90 || mPoint.y+yy <= 40 || mPoint.y+yy >= 440){ if((mPoint.x+xx <= 90 && mPoint.y+yy >= 440) || (mPoint.x+xx <= 90 && mPoint.y+yy <= 40) || (mPoint.x+xx >= 770 && mPoint.y+yy >= 440) || (mPoint.x+xx >= 770 && mPoint.y+yy <= 40)){ m_player->setposition(mPoint.x,mPoint.y); }else{ if(mPoint.x+xx >= 770 && xx > 0){ m_player->setpositionY(mPoint.y+yy); } if(mPoint.x+xx <= 90 && xx < 0){ m_player->setpositionY(mPoint.y+yy); } if(mPoint.y+yy >= 440 && yy > 0){ m_player->setpositionX(mPoint.x+xx); } if(mPoint.y+yy <= 40 && yy < 0){ m_player->setpositionX(mPoint.x+xx); } } }else{ m_player->setposition(ccp(mPoint.x+xx,mPoint.y+yy)); }}voID MainScene::cctouchended(CCtouch *ptouch,CCEvent *pEvent){ this->setSpeed(0,0);}voID MainScene::registerWithtouchdispatcher(){ CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,true);}</span>总结
以上是内存溢出为你收集整理的cocos2dx实现手指控制物体移动全部内容,希望文章能够帮你解决cocos2dx实现手指控制物体移动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)