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

简介: #ifndef __HELLOWORLD_SCENE_H__   #define __HELLOWORLD_SCENE_H__      #include "cocos2d.

  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5. using namespace cocos2d;  
  6.   
  7. class HelloWorld : public cocos2d::CCLayerColor  
  8. {  
  9. public:  
  10.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)  
  11.     virtual bool init();  
  12.   
  13.     // there's no 'id' in cpp, so we recommend to return the class instance pointer  
  14.     static cocos2d::CCScene* scene();  
  15.       
  16.     // a selector callback  
  17.     void menuCloseCallback(CCObject* pSender);  
  18.   
  19.     // preprocessor macro for "static create()" constructor ( node() deprecated )  
  20.     CREATE_FUNC(HelloWorld);  
  21.       
  22.     void addTarget();  
  23.     void spriteMoveFinished(CCNode *sender);  
  24.     void gameLogic(cocos2d::CCTime dt);  
  25.       
  26.     void ccTouchesEnded(CCSet *touches, CCEvent *event);  
  27.       
  28.     CCArray *aarayTarget;//敌人  
  29.     CCArray *arrayProjectile;//飞镖  
  30.     void update(CCTime dt);  
  31. };  
  32.   
  33. #endif // __HELLOWORLD_SCENE_H__  

HelloWorldScence.cpp

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "HelloWorldScene.h"  
  2. #include "SimpleAudioEngine.h"  
  3.   
  4. using namespace cocos2d;  
  5. using namespace CocosDenshion;  
  6.   
  7. CCScene* HelloWorld::scene()  
  8. {  
  9.     // 'scene' is an autorelease object  
  10.     CCScene *scene = CCScene::create();  
  11.       
  12.     // 'layer' is an autorelease object  
  13.     HelloWorld *layer = HelloWorld::create();  
  14.   
  15.     // add layer as a child to scene  
  16.     scene->addChild(layer);  
  17.   
  18.     // return the scene  
  19.     return scene;  
  20. }  
  21.   
  22.   
  23.   
  24. // on "init" you need to initialize your instance  
  25. bool HelloWorld::init()  
  26. {  
  27.     //////////////////////////////  
  28.     // 1. super init first  
  29.     if ( CCLayerColor::initWithColor(ccc4(255255255255)) )  
  30.     {  
  31.           
  32.         CCSize winSize = CCDirector::sharedDirector()->getWinSize();//获取屏幕大小  
  33.           
  34.         arrayProjectile = CCArray::create();  
  35.         aarayTarget = CCArray::create();  
  36.           
  37.         float sprite_scale = 2.0;  
  38.         CCSprite *Player =  CCSprite::create("Player.png");  
  39.         Player->setScale(sprite_scale);  
  40.         Player->setPosition(ccp(Player->getContentSize().width*sprite_scale/2.0, winSize.height/2.0));  
  41.         this->addChild(Player);  
  42.           
  43.         aarayTarget->retain();  
  44.         arrayProjectile->retain();  
  45.           
  46.         this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);  
  47.         this->schedule(schedule_selector(HelloWorld::update));  
  48.         this->setTouchEnabled(true);  
  49.           
  50.         return true;  
  51.     }  
  52.     else{  
  53.         return false;  
  54.     }  
  55.   
  56.       
  57. }  
  58.   
  59. void HelloWorld::gameLogic(cocos2d::CCTime dt){  
  60.     this->addTarget();  
  61. }  
  62.   
  63.   
  64.   
  65. void HelloWorld::addTarget(){  
  66.       
  67.     CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
  68.       
  69.     CCSprite *target = CCSprite::create("Target.png");  
  70.       
  71.     //随机位置  
  72.     int minY = target->getContentSize().height/2.0;  
  73.     int maxY = winSize.height - target->getContentSize().height/2.0;  
  74.     int rangeY = maxY - minY;  
  75.     int actualY = rand()%rangeY + minY;  
  76.       
  77.     target->setPosition(ccp(winSize.width - target->getContentSize().width/2.0, actualY));  
  78.     target->setTag(1);  
  79.     this->addChild(target);  
  80.     aarayTarget->addObject(target);  
  81.       
  82.     //随机速度  
  83.     float minDuration = 2.0;  
  84.     float maxDuration = 4.0;  
  85.     int rangeDuration = maxDuration - minDuration;  
  86.     float actualDuration = rand()%rangeDuration + minDuration;  
  87.       
  88.       
  89.       
  90.       
  91.     CCFiniteTimeAction *actionMove = CCMoveTo::create(actualDuration, ccp(0 - target->getContentSize().width/2.0, actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外  
  92.       
  93.       
  94.     //增加一个回调函数,回收移动到屏幕外的精灵  
  95.     CCFiniteTimeAction *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));  
  96.     target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));  
  97.       
  98. }  
  99.   
  100. void HelloWorld::spriteMoveFinished(cocos2d::CCNode *sender){  
  101.     CCSprite *sprite = (CCSprite *)sender;  
  102.    // this->removeChild(sprite, true);  
  103.     if (sprite->getTag() == 1) {  
  104.         aarayTarget->removeObject(sprite);//清除敌人  
  105.     }else if(sprite->getTag() == 2){  
  106.         arrayProjectile->removeObject(sprite);//清除飞镖  
  107.     }  
  108. }  
  109.   
  110.   
  111. //发射飞镖  
  112. void HelloWorld::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *event){  
  113.     CCTouch *touch = (CCTouch *)touches->anyObject();  
  114.     CCPoint location = touch->getLocationInView();  
  115.     location = this->convertTouchToNodeSpace(touch);  
  116.       
  117.     CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
  118.       
  119.     CCSprite *projectile = CCSprite::create("Projectile.png");  
  120.     projectile->setPosition(ccp(20, winSize.height/2));  
  121.       
  122.     float offX = location.x - projectile->getPositionX();  
  123.     float offY = location.y - projectile->getPositionY();  
  124.       
  125.     if (offX <= 0) {  
  126.         return;  
  127.     }  
  128.     projectile->setTag(2);  
  129.     this->addChild(projectile);  
  130.     arrayProjectile->addObject(projectile);  
  131.       
  132.     float angle = offY/offX;  
  133.     float realX= winSize.width + projectile->getContentSize().width/2;  
  134.     float realY = realX * angle + projectile->getPositionY();  
  135.       
  136.     CCPoint finalPosition = ccp(realX, realY);  
  137.       
  138.     //获取飞镖飞行时间  
  139.     float length = sqrtf(realX *realX + realY*realY);  
  140.     float velocity = 480;  
  141.     float realMoveDuration = length/velocity;  
  142.       
  143.     projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, finalPosition),CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL));  
  144. }  
  145.   
  146. //碰撞检测,消除飞镖和敌人  
  147. void HelloWorld::update(cocos2d::CCTime dt){  
  148.     for (int i = 0; i < aarayTarget->count(); i++) {  
  149.         CCSprite *target = (CCSprite *)aarayTarget->objectAtIndex(i);  
  150.         for (int j = 0; j < arrayProjectile->count(); j++) {  
  151.             CCSprite *projectile = (CCSprite *)arrayProjectile->objectAtIndex(j);  
  152.             if (target->boundingBox().intersectsRect(projectile->boundingBox())) {  
  153.                 aarayTarget->removeObjectAtIndex(i);  
  154.                 arrayProjectile->removeObjectAtIndex(j);  
  155.                 this->removeChild(target);  
  156.                 this->removeChild(projectile);  
  157.                   
  158.                 break;  
  159.             }  
  160.         }  
  161.     }  
  162. }  
  163.   
  164.   
  165.   
  166. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  167. {  
  168.     CCDirector::sharedDirector()->end();  
  169.   
  170. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  171.     exit(0);  
  172. #endif  
  173. }  
相关文章
关于 qml程序在目标机上开发时运行出现不断闪屏现象的 解决方法
关于 qml程序在目标机上开发时运行出现不断闪屏现象的 解决方法
关于 qml程序在目标机上开发时运行出现不断闪屏现象的 解决方法
|
9月前
怎么删除360base64.dll,这个方法百分百管用
怎么删除360base64.dll,这个方法百分百管用
225 0
|
12月前
|
Python
python植物大战僵尸二十二之僵尸攻击状态改变
python植物大战僵尸二十二之僵尸攻击状态改变
66 0
|
iOS开发 开发者
Xcode关于警告AutomaticPreferredMaxLayoutWidth的消除方法
Xcode关于警告AutomaticPreferredMaxLayoutWidth的消除方法
122 0
Xcode关于警告AutomaticPreferredMaxLayoutWidth的消除方法