返回首页 cocos2d-x 入门实战-微信飞机大战讲解

UFO层特殊道具

游戏的趣味性就在于不时的给你一些惊喜。当然如果只是简单的子弹打飞机,玩久了也会腻,所以加入一些好玩的道具可以很好的提高游戏的可玩性。

飞机大战中也有不时从天而降的UFO(UFO是腾讯给他取的,我也是从游戏资源里发现这么个名字,照搬啦)。这里主要有两种道具,双排子弹和大炸弹。

1.多子弹层

多子弹和单子弹是一样的实现。请参照第五篇和第六篇的处理,这里就不附代码了。

要注意的地方有:

(1)使用CCSpriteBatchNode处理提高渲染效率。

(2)子弹的初始位置,有两排,和单子弹层略有不同。

(3)子弹的飞行效果和回收,这和单子弹层是一样的。

(4)其他差别就是精灵图片不一样,发射频率不一样,多子弹是有时限的,不能是CCRepeateForever了。

2.UFO层

UFO层有两类精灵道具:

(1)ufo1,蓝色降落伞道具,吃到了就有多子弹效果。

(2)ufo2,红色降落伞道具,吃到了屏幕左下角会出现炸弹数,点击会全屏秒杀。

这里以多子弹道具的掉落为例。炸弹道具的掉落类似不赘述。

2.1.添加多子弹降落伞道具

    void UFOLayer::AddMutiBullets(float dt)
    {
        //创建多子弹道具精灵
        CCSprite* mutiBullets=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("ufo1.png"));
        //获得随机位置
        CCSize mutiBlletsSize=mutiBullets->getContentSize();
        CCSize winSize=CCDirector::sharedDirector()->getWinSize();
        int minX=mutiBlletsSize.width/2;
        int maxX=winSize.width-mutiBlletsSize.width/2;
        int rangeX=maxX-minX;
        int actualX=(rand()%rangeX)+minX;
        //添加到UFO层并加入CCArray数组
        mutiBullets->setPosition(ccp(actualX,winSize.height+mutiBlletsSize.height/2));
        this->addChild(mutiBullets);
        this->m_pAllMutiBullets->addObject(mutiBullets);//m_pAllMutibullets是CCArray数组指针,成员变量
        //这是一个比较cute的动画,也可以采用CCEaseAction类处理
        CCMoveBy* move1 = CCMoveBy::create(0.5f, CCPointMake(0, -150));
        CCMoveBy* move2 = CCMoveBy::create(0.3f, CCPointMake(0, 100));
        CCMoveBy* move3 = CCMoveBy::create(1.0f, CCPointMake(0,0-winSize.height-mutiBlletsSize.height/2));
        CCCallFuncN* moveDone = CCCallFuncN::create(this,callfuncN_selector(UFOLayer::mutiBulletsMoveFinished));
        CCFiniteTimeAction* sequence = CCSequence::create(move1,move2,move3,moveDone,NULL);
        //执行动画
        mutiBullets->runAction(sequence);
    }

在init()用schedule来控制道具掉落的频率。

2.2.多子弹降落伞道具的回收

如果玩家未吃到该道具,在超出屏幕范围后,自动回收。

    void UFOLayer::mutiBulletsMoveFinished(CCNode* pSender)
    {
        CCSprite* mutiBullets=(CCSprite*)pSender;
        this->removeChild(mutiBullets,true);//从屏幕中移除
        this->m_pAllMutiBullets->removeObject(mutiBullets);//从数组中移除
    }

2.3.提供的移除接口

如果玩家吃到该道具,调用此接口,移除并回收该道具。

    void UFOLayer::RemoveMutiBullets(CCSprite* mutiBullets)
    {
        this->removeChild(mutiBullets,true);
        this->m_pAllMutiBullets->removeObject(mutiBullets);
    }

3.碰撞检测

在GameLayer的update中执行主角与ufo的碰撞检测。

    //mutiBullets & airplane CheckCollision
    CCObject *ut;
    CCARRAY_FOREACH(this->ufoLayer->m_pAllMutiBullets,ut)
    {
        CCSprite* mutiBullets=(CCSprite*)ut;
        if (CCRect::CCRectIntersectsRect(PlaneLayer::sharedPlane->boundingBox(),mutiBullets->boundingBox()))//玩家吃到双子弹道具
        {
            this->ufoLayer->RemoveMutiBullets(mutiBullets);//调用移除双子弹道具
            this->bulletLayer->StopShoot();//停止单子弹的射击
            this->mutiBulletsLayer->StartShoot();//开启双子弹的射击
            this->bulletLayer->StartShoot(6.2f);//还记得在第四篇StartShoot我们将它设置为缺省函数?这样一来就可以实现双子弹结束后切换回单子弹射击
        }
    }

4.炸弹ufo的使用

给GameLayer加个成员变量,int bigBoomCount,初始为0,代表主角飞机当前拥有的炸弹数,吃到了(碰撞检测)就+1,使用(触摸炸弹按钮)-1。当多个炸弹就在炸弹图标边上 x N。。。

给GameLayer增加如下函数,当检测主角飞机碰撞炸弹ufo时调用。

    //更新炸弹图标和数量显示
    void GameLayer::updateBigBoomItem(int bigBoomCount)//传入当前炸弹数
    {
        CCSprite* normalBomb=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("bomb.png"));
        CCSprite* pressedBomb=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("bomb.png"));
        if (bigBoomCount<0)
        {
            return;
        }
        else if (bigBoomCount==0)
        {
            if(this->getChildByTag(TAG_BIGBOOM_MENUITEM))
            {
                this->removeChildByTag(TAG_BIGBOOM_MENUITEM,true);//移除炸弹图标
            }
            if (this->getChildByTag(TAG_BIGBOOMCOUNT_LABEL))
            {
                this->removeChildByTag(TAG_BIGBOOMCOUNT_LABEL,true);//移除炸弹数量
            }
        }
        else if (bigBoomCount==1)
        {
            //加入bigBoomItemMenu
            if (!this->getChildByTag(TAG_BIGBOOM_MENUITEM))
            {
                CCMenuItemImage* pBigBoomItem=CCMenuItemImage::create();
                pBigBoomItem->initWithNormalSprite(normalBomb,pressedBomb,NULL,this,menu_selector(GameLayer::menuBigBoomCallback));
                pBigBoomItem->setPosition(ccp(normalBomb->getContentSize().width/2+10,normalBomb->getContentSize().height/2+10));
                menuBigBoom=CCMenu::create(pBigBoomItem,NULL);
                menuBigBoom->setPosition(CCPointZero);
                this->addChild(menuBigBoom,0,TAG_BIGBOOM_MENUITEM);
            }
            if (this->getChildByTag(TAG_BIGBOOMCOUNT_LABEL))
            {
                this->removeChildByTag(TAG_BIGBOOMCOUNT_LABEL,true);//移除炸弹数量
            }
        }
        else
        {
            if (!this->getChildByTag(TAG_BIGBOOM_MENUITEM))
            {
                CCMenuItemImage* pBigBoomItem=CCMenuItemImage::create();
                pBigBoomItem->initWithNormalSprite(normalBomb,pressedBomb,NULL,this,menu_selector(GameLayer::menuBigBoomCallback));
                pBigBoomItem->setPosition(ccp(normalBomb->getContentSize().width/2+10,normalBomb->getContentSize().height/2+10));
                menuBigBoom=CCMenu::create(pBigBoomItem,NULL);
                menuBigBoom->setPosition(CCPointZero);
                this->addChild(menuBigBoom,0,TAG_BIGBOOM_MENUITEM);
            }
            if (this->getChildByTag(TAG_BIGBOOMCOUNT_LABEL))
            {
                this->removeChildByTag(TAG_BIGBOOMCOUNT_LABEL,true);
            }
            if (bigBoomCount>=0 && bigBoomCount<=MAX_BIGBOOM_COUNT)//MAX_BIGBOOM_COUNT设置为100000,够用了。。。打出来手都长茧了。
            {
                CCString* strScore=CCString::createWithFormat("X%d",bigBoomCount);//增加炸弹数量显示
                bigBoomCountItem=CCLabelBMFont::create(strScore->m_sString.c_str(),"font/font.fnt");//自定义字体
                bigBoomCountItem->setColor(ccc3(143,146,147));
                bigBoomCountItem->setAnchorPoint(ccp(0,0.5));
                bigBoomCountItem->setPosition(ccp(normalBomb->getContentSize().width+15,normalBomb->getContentSize().height/2+5));
                this->addChild(bigBoomCountItem,0,TAG_BIGBOOMCOUNT_LABEL);
            }
        }
    }

使用炸弹触发的回调函数

    //使用炸弹的回调函数
    void GameLayer::menuBigBoomCallback(CCObject* pSender)
    {
        if(bigBoomCount>0 && !CCDirector::sharedDirector()->isPaused())//炸弹数大于0,且游戏未暂停,参见后一篇:游戏暂停和触摸屏蔽
        {
            bigBoomCount--;//炸弹数-1
            this->enemyLayer->removeAllEnemy();//敌机全挂掉
            updateBigBoomItem(bigBoomCount);//使用后更新炸弹显示
        }
    }

效果图