cocos2d-x入门(4)-英雄发射子弹和碰撞检测

现在来让我们的英雄发射子弹,主要需要监听屏幕触摸事件,根据触摸点的坐标和英雄的位置决定子弹行走路线。然后对子弹和屏幕上的敌人进行碰撞检测,若击中敌人则把子弹和敌人都从该层清除。

在头文件GameScene.h中添加如下代码:

void update(cocos2d::ccTime dt);
 
        void ccTouchesEnded(cocos2d::CCSet *touches,cocos2d::CCEvent *event);
 
private:
    cocos2d::CCMutableArray<cocos2d::CCSprite *> *enemies;
    cocos2d::CCMutableArray<cocos2d::CCSprite *> *projectiles;
添加了两个成员变量:enemies和projectiles,这两个容器是用来存储屏幕上的敌人和子弹的,为后面的碰撞检测做准备。
修改源文件GameScene.cpp:
在init函数中添加:
enemies = new CCMutableArray<CCSprite *>;
projectiles = new CCMutableArray<CCSprite *>;
this->setIsTouchEnabled(true);
this->schedule(schedule_selector(SimpleGame::update));

定时器不指定时间表示每帧都调用。

在addEnemy函数中添加:

enemy->setTag(1);
enemies->addObject(enemy);

设置敌人精灵tag为1,子弹精灵tag为2。

在析构函数~SimpleGame中添加:

if(enemies)
{
    enemies->release();  
    enemies = NULL;
}
 
if(projectiles)
{
    projectiles->release();  
    projectiles = NULL;
}

在spriteMoveFinished函数中添加:
if(sprite->getTag() == 1)
{
    enemies->removeObject(sprite);       
}
else if(sprite->getTag() == 2)
{
    projectiles->removeObject(sprite);   
}

添加update和ccTouchesEnded函数:监听触屏事件和实现碰撞检测。
void SimpleGame::ccTouchesEnded(CCSet *touches,CCEvent *event)
{
    //CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
    CCTouch *touch = (CCTouch *)(touches->anyObject());
    CCPoint location = touch->locationInView(touch->view());
    location = CCDirector::sharedDirector()->convertToGL(location);
 
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png",CCRectMake(0,0,20,20));
    projectile->setPosition(ccp(20,winSize.height / 2));
 
    int offX = location.x - projectile->getPosition().x;
    int offY = location.y - projectile->getPosition().y;
    if(offX <= 0) return;
 
    this->addChild(projectile);
 
    int realX = winSize.width + projectile->getPosition().x / 2;
    float ratio = (float)offY / (float)offX;
    int realY = realX * ratio + projectile->getPosition().y;
 
    int realOffX = realX - projectile->getPosition().x;
    int realOffY = realY - projectile->getPosition().y;
    float distance = sqrtf(realOffX * realOffX + realOffY * realOffY);
    float velocity = 480 / 1;
    float realMoveDuration = distance / velocity;
    projectile->runAction(CCSequence::actions(CCMoveTo::actionWithDuration(realMoveDuration,ccp(realX,realY)),
                                                                   CCCallFuncN::actionWithTarget(this,callfuncN_selector(SimpleGame::spriteMoveFinished))
                                         ,NULL));
 
    projectile->setTag(2);
    projectiles->addObject(projectile);
 
}
 
void SimpleGame::update(ccTime dt)
{
    CCMutableArray<CCSprite *>::CCMutableArrayIterator it,jt;
    CCMutableArray<CCSprite *> *projectileToDelete = new CCMutableArray<CCSprite *>;
    //遍历所有子弹,然后根据子弹遍历所有敌人,检查子弹跟敌人是否发生碰撞
    for(jt = projectiles->begin(); jt != projectiles->end(); jt ++)
    {
        CCSprite *projectile = *jt;
        CCRect projectileRect = CCRectMake(projectile->getPosition().x - projectile->getContentSize().width/2,
                                       projectile->getPosition().y - projectile->getContentSize().height/2,
                                       projectile->getContentSize().width,
                                       projectile->getContentSize().height);
        CCMutableArray<CCSprite *> *enemyToDelete = new CCMutableArray<CCSprite *>;
        for(it = enemies->begin(); it != enemies->end(); it ++)
        {
            CCSprite *enemy = *it;
            CCRect enemyRect = CCRectMake(enemy->getPosition().x - enemy->getContentSize().width/2,
                                       enemy->getPosition().y - enemy->getContentSize().height/2,
                                       enemy->getContentSize().width,
                                       enemy->getContentSize().height);
            //如果碰撞,则把被击中的敌人放进要删除的列表中
            if(CCRect::CCRectIntersectsRect(projectileRect,enemyRect))
            {
                enemyToDelete->addObject(enemy); 
            }
        }
 
        for(it = enemyToDelete->begin(); it != enemyToDelete->end(); it ++)
        {
            CCSprite *enemy = *it;
            enemies->removeObject(enemy);
            this->removeChild(enemy,true);
        }
 
        //无论该子弹击中多少敌人,都只添加一次
        if(enemyToDelete->count() > 0)
        {
            projectileToDelete->addObject(projectile);
        }
 
        enemyToDelete->release();
    }
 
    for(jt = projectileToDelete->begin(); jt != projectileToDelete->end(); jt ++)
    {
        CCSprite *projectile = *jt;
        projectiles->removeObject(projectile);
        this->removeChild(projectile,true);
    }
    projectileToDelete->release();
 
}

编译运行程序,现在可以把敌人一个一个的干掉了。



转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/cocos2d-x/690.html

原文链接:加载失败,请重新获取