cocos2d-x 植物大战僵尸(三) bool型值对游戏流程的控制

来源:互联网 发布:数据科学 北大 编辑:程序博客网 时间:2024/04/30 08:52

当植物枪产生以后要进入一个冷却时间;在这段时间内不允许产生新的植物枪;这需要一个bool型值来控制;

声明一个bool型变量 

 bool _control;//这个变量用来控制倒计时过程中,禁止产生植物抢;

初始化为false;

下面这段代码主要是实现: 当发生触摸时产生植物枪 ,它有三个约束条件 (触摸点必须在卡片上, 金币必须大于$100  ,下面一个条件就是我们的bool型变量  它必须是false的时候才可以产生植物枪)



冷却时间内点击卡片不能产生植物枪


当冷却时间为零时。销毁冷却时间板。设置冷却时间为50s ,又可以产生植物枪了


void GameLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCTouch* touch = (CCTouch*)pTouches->anyObject();
if(this->_shootCard->boundingBox().containsPoint(touch->getLocation()) && this->_allScore>=100 && this->_control==false)
{

            //创建植物枪
this->_plantSprite =PlantSprite::create();
this->_batchNode_Plant->addChild(this->_plantSprite);

       // 每个植物枪消耗100金币
this->_allScore =this->_allScore-100;

       //改变植物枪和卡片的z轴顺序
  this->reorderChild(this->_batchNode_Plant,10);

      //设置植物枪产生的位置为触摸点位置
POINT        tCurrPos;
::GetCursorPos(&tCurrPos);
CCEGLView*        pGELView = CCDirector::sharedDirector()->getOpenGLView();
::ScreenToClient(pGELView->getHWnd(),&tCurrPos);
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
this->_plantSprite->setPosition(ccp(tCurrPos.x/pGELView->getFrameZoomFactor() ,visibleSize.height - tCurrPos.y/pGELView->getFrameZoomFactor()));

this->draw();

    //植物枪产生后进入冷却时间
this->is_create =true;
if(is_create)
{
//实现倒计时
this->_down_Label =CCLabelTTF::create();
CCString* downStr = CCString::createWithFormat("%d",this->_dowmTime);
this->_down_Label->setString(downStr->m_sString.c_str());
this->addChild(this->_down_Label,3);
this->_down_Label->setPosition(ccp(this->_shootCard->getPosition().x,this->_shootCard->getPosition().y));
this->_down_Label->setFontSize(30);
schedule(schedule_selector(GameLayer::downTime),1.0f);  //实现倒计时
}
}
}


//更新倒计时/更新冷却时间 (在冷却时间内设置我们的bool形变量为true ,这样就不可以产生植物枪了)
void GameLayer::downTime(float dt)
{
this->_dowmTime =this->_dowmTime -1;
CCString* downStr = CCString::createWithFormat("%d",this->_dowmTime);
this->_down_Label->setString(downStr->m_sString.c_str());
if(this->_dowmTime<50)
{
this->_control =true;
}
if(this->_dowmTime==0)   //当冷却时间为零时 ,销毁冷却时间版 。。将bool变量设为false;
{
unschedule(schedule_selector(GameLayer::downTime));
this->removeChild(this->_down_Label);
this->_dowmTime =50;
this->_control=false;
}







}



原创粉丝点击