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

当植物枪产生以后要进入一个冷却时间;在这段时间内不允许产生新的植物枪;这需要一个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;
}







}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值