Plants vs. Zombies cocos2d-x (8) to achieve cooling time countdown

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Today because the party. Blog is relatively late, sorry to have kept you waiting. Well do not explain, explain is to conceal, men do not need to explain! Ha-ha.

Here today to share with you is (cooling time / countdown) implementation. This is a lot of fun Oh, all process control logic, the code I probably changed 2 times, is the test of a person (the logical thinkingI thought for a moment, I decided to write to share). I'm going to implement it in the last time we have written in TouchLayer, the chapter is not very skilled friends can look down. Why must I have stressed the need to implement it in TouchLayer, the main reason is because the static binding schedule with this method, he demanded that we can only realize in this document. Don't talk about these theories, it may seem very dull! Here we go,

First we open the base CardAstributeSprite class all plants card, this class in several articles we have created. I just said at the time that temporarily

Don't know what to put into the air, where the,

We add a line of code in CardAstributeSprite.h:

int _timeLost;//The total time countdown


Then we in plant card wizard class initializes the integer variable:

Is in the CardSprite.cpp inside the constructor: add a line of code:

this->_timeLost =50;

Below we open our TouchLayer.h file: add the following code inside:

//Because the schedule bindings static methods, so we created in the touch layer, the countdown
	bool _isCreate ;////For process control, determine whether plants have gun, 
	void createTimeLabel();//Create a countdown board function
	void timeLost(float dt);//The countdown
	cocos2d::CCLabelTTF* _timeLabel;//Down time drawing board
	cocos2d::CCString*  _timeStr;//The countdown string converted, 

In the constructor of the TouchLayer.cpp: add a line of code:


this->_isCreate =false;//Initialize default gun without generating plant

In the void TouchLayer:: createTimeLabel () to add the following code:


void TouchLayer::createTimeLabel()
{
	this->_timeLabel =CCLabelTTF::create();//Create dynamic drawing board
	this->_timeStr =CCString::createWithFormat("%d",((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost);//The integer into a string type
	this->_timeLabel->setString(this->_timeStr->m_sString.c_str());//Gets a string object and transformed into a char* type
	this->addChild(this->_timeLabel);
	this->setPosition(((GameLayer*)this->getParent())->_cardLayer->_cardSprite->getPosition());
	this->_timeLabel->setFontSize(26);
	schedule(schedule_selector(TouchLayer::timeLost),1.0f);

   
}

In: void TouchLayer:: timeLost (float DT) to add the following code:

void TouchLayer::timeLost(float dt)
{
	//The following are the type conversion, 
	this->_timeStr =CCString::createWithFormat("%d",((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost);//The integer into a string type
	this->_timeLabel->setString(this->_timeStr->m_sString.c_str());//Gets a string object and transformed into a char* type
	((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost= ((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost-1;//Let the integer time every second., 

	if(((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost<=50)
	{
		this->_isPeaShooter =true; //At this time that cannot produce plant.
	}

	if(((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost == 0)
	{
		this->unschedule(schedule_selector(TouchLayer::timeLost));
		this->removeChild(this->_timeLabel,true);
		((GameLayer*)this->getParent())->_cardLayer->_cardSprite->_timeLost =50;
		this->_isPeaShooter = false;
	}


}

In the void TouchLayer:: ccTouchesBegan (CCSet *pTouches, CCEvent *pEvent) to add the following code:
void TouchLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
   CCTouch* pTouch = (CCTouch*)pTouches->anyObject();//Multi touch to touch a single point
   if(((GameLayer*)this->getParent())->_cardLayer->_cardSprite->boundingBox().containsPoint(pTouch->getLocation()) &&((GameLayer*)this->getParent())->_dollarDisplayLayer->_dollar>=100 && this->_isPeaShooter ==false)//Judge whether the card touch occur in plants
   {
	   ((GameLayer*)this->getParent())->_peaShooterLayer->initPeaShooterSprite(pTouch); 
	    this->_isCreate =true;//The plant has said the gun

		  //If a plant the gun has generated we call timer, to achieve the countdown, 
		if(this->_isCreate == true)
		{
			this->createTimeLabel();

		}

   }
 
  

}
In: void TouchLayer:: ccTouchesMoved (cocos2d:: CCSet *pTouches, cocos2d:: CCEvent *pEvent) add the following code:
void TouchLayer::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCTouch* pTouch = (CCTouch*)pTouches->anyObject();//Multi touch to touch a single point
	if(((GameLayer*)this->getParent())->_peaShooterLayer->_peaShooterSprite->boundingBox().containsPoint(pTouch->getLocation()))
	{
		((GameLayer*)this->getParent())->_peaShooterLayer->_peaShooterSprite->setPosition(pTouch->getLocation());
	}

		

}

So the countdown function is complete, we look at the effect chart:


In the cooling time countdown click plant card, do not produce the plant.:


When the countdown to zero, the destruction of the countdown.:


When gold is more than 100 and click the plant card, will appear again the countdown:



This for me to achieve, is easy, so when you write, not too much attention that's the point, where we are not very clear, can mention in the comments... If you read my previous article, should not be a problem!


Tonight is here,, thank you support!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Rod at December 13, 2013 - 1:53 AM