Cocos2d-x碰撞检查与消灭的实现

前端之家收集整理的这篇文章主要介绍了Cocos2d-x碰撞检查与消灭的实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. #ifndef__HELLOWORLD_SCENE_H__
  2. #define__HELLOWORLD_SCENE_H__
  3. #include"cocos2d.h"
  4. usingnamespacecocos2d;
  5. classHelloWorld:publiccocos2d::CCLayerColor
  6. {
  7. public:
  8. //Method'init'incocos2d-xreturnsbool,insteadof'id'incocos2d-iphone(anobjectpointer)
  9. virtualboolinit();
  10. //there'sno'id'incpp,sowerecommendtoreturntheclassinstancepointer
  11. staticcocos2d::CCScene*scene();
  12. //aselectorcallback
  13. voidmenuCloseCallback(CCObject*pSender);
  14. //preprocessormacrofor"staticcreate()"constructor(node()deprecated)
  15. CREATE_FUNC(HelloWorld);
  16. voidaddTarget();
  17. voidspriteMoveFinished(CCNode*sender);
  18. voidgameLogic(cocos2d::CCTimedt);
  19. voidccTouchesEnded(CCSet*touches,CCEvent*event);
  20. CCArray*aarayTarget;//敌人
  21. CCArray*arrayProjectile;//飞镖
  22. voidupdate(CCTimedt);
  23. };
  24. #endif//__HELLOWORLD_SCENE_H__

HelloWorldScence.cpp

    #include"HelloWorldScene.h"
  1. #include"SimpleAudioEngine.h"
  2. namespacecocos2d;
  3. namespaceCocosDenshion;
  4. CCScene*HelloWorld::scene()
  5. //'scene'isanautoreleaSEObject
  6. CCScene*scene=CCScene::create();
  7. //'layer'isanautoreleaSEObject
  8. HelloWorld*layer=HelloWorld::create();
  9. //addlayerasachildtoscene
  10. scene->addChild(layer);
  11. //returnthescene
  12. returnscene;
  13. }
  14. //on"init"youneedtoinitializeyourinstance
  15. boolHelloWorld::init()
  16. //////////////////////////////
  17. //1.superinitfirst
  18. if(CCLayerColor::initWithColor(ccc4(255,255,255)))
  19. CCSizewinSize=CCDirector::sharedDirector()->getWinSize();//获取屏幕大小
  20. arrayProjectile=CCArray::create();
  21. aarayTarget=CCArray::create();
  22. floatsprite_scale=2.0;
  23. CCSprite*Player=CCSprite::create("Player.png");
  24. Player->setScale(sprite_scale);
  25. Player->setPosition(ccp(Player->getContentSize().width*sprite_scale/2.0,winSize.height/2.0));
  26. this->addChild(Player);
  27. aarayTarget->retain();
  28. arrayProjectile->retain();
  29. this->schedule(schedule_selector(HelloWorld::gameLogic),1.0);
  30. this->schedule(schedule_selector(HelloWorld::update));
  31. this->setTouchEnabled(true);
  32. returntrue;
  33. }
  34. else{
  35. false;
  36. voidHelloWorld::gameLogic(cocos2d::CCTimedt){
  37. this->addTarget();
  38. HelloWorld::addTarget(){
  39. CCSizewinSize=CCDirector::sharedDirector()->getWinSize();
  40. CCSprite*target=CCSprite::create("Target.png");
  41. //随机位置
  42. intminY=target->getContentSize().height/2.0;
  43. intmaxY=winSize.height-target->getContentSize().height/2.0;
  44. intrangeY=maxY-minY;
  45. intactualY=rand()%rangeY+minY;
  46. target->setPosition(ccp(winSize.width-target->getContentSize().width/2.0,actualY));
  47. target->setTag(1);
  48. this->addChild(target);
  49. aarayTarget->addObject(target);
  50. //随机速度
  51. floatminDuration=2.0;
  52. floatmaxDuration=4.0;
  53. intrangeDuration=maxDuration-minDuration;
  54. floatactualDuration=rand()%rangeDuration+minDuration;
  55. CCFiniteTimeAction*actionMove=CCMoveTo::create(actualDuration,ccp(0-target->getContentSize().width/2.0,actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外
  56. //增加一个回调函数,回收移动到屏幕外的精灵
  57. CCFiniteTimeAction*actionMoveDone=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::spriteMoveFinished));
  58. target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));
  59. HelloWorld::spriteMoveFinished(cocos2d::CCNode*sender){
  60. CCSprite*sprite=(CCSprite*)sender;
  61. //this->removeChild(sprite,true);
  62. if(sprite->getTag()==1){
  63. aarayTarget->removeObject(sprite);//清除敌人
  64. }elseif(sprite->getTag()==2){
  65. arrayProjectile->removeObject(sprite);//清除飞镖
  66. //发射飞镖
  67. HelloWorld::ccTouchesEnded(cocos2d::cocos2d::CCEvent*event){
  68. CCTouch*touch=(CCTouch*)touches->anyObject();
  69. CCPointlocation=touch->getLocationInView();
  70. location=this->convertTouchToNodeSpace(touch);
  71. CCSprite*projectile=CCSprite::create("Projectile.png");
  72. projectile->setPosition(ccp(20,winSize.height/2));
  73. floatoffX=location.x-projectile->getPositionX();
  74. floatoffY=location.y-projectile->getPositionY();
  75. if(offX<=0){
  76. return;
  77. projectile->setTag(2);
  78. this->addChild(projectile);
  79. arrayProjectile->addObject(projectile);
  80. floatangle=offY/offX;
  81. floatrealX=winSize.width+projectile->getContentSize().width/2;
  82. floatrealY=realX*angle+projectile->getPositionY();
  83. CCPointfinalPosition=ccp(realX,realY);
  84. //获取飞镖飞行时间
  85. floatlength=sqrtf(realX*realX+realY*realY);
  86. floatvelocity=480;
  87. floatrealMoveDuration=length/velocity;
  88. projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration,finalPosition),CCCallFuncN::create(this,callfuncN_selector(HelloWorld::spriteMoveFinished)),153); background-color:inherit; font-weight:bold">NULL));
  89. //碰撞检测,消除飞镖和敌人
  90. HelloWorld::update(cocos2d::CCTimedt){
  91. for(inti=0;i<aarayTarget->count();i++){
  92. CCSprite*target=(CCSprite*)aarayTarget->objectAtIndex(i);
  93. intj=0;j<arrayProjectile->count();j++){
  94. CCSprite*projectile=(CCSprite*)arrayProjectile->objectAtIndex(j);
  95. if(target->boundingBox().intersectsRect(projectile->boundingBox())){
  96. aarayTarget->removeObjectAtIndex(i);
  97. arrayProjectile->removeObjectAtIndex(j);
  98. this->removeChild(target);
  99. this->removeChild(projectile);
  100. break;
  101. HelloWorld::menuCloseCallback(CCObject*pSender)
  102. {
  103. CCDirector::sharedDirector()->end();
  104. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
  105. exit(0);
  106. #endif
  107. }
原文链接:/cocos2dx/345068.html

猜你在找的Cocos2d-x相关文章