[Wood Cocos2d-x 035] I am a timer (Chapter 1)-Stop update and trigger

Source: Internet
Author: User

I am a timer (Chapter 1)-Stop update and trigger

 

Sometimes we don't want to execute a function constantly. We only want to execute a function in 20 seconds and then don't execute it. For example, if we use a microwave oven to bake tomatoes, it will take 3 minutes to ring, but it will be nice if we only need to ring it. It will not sound every 3 minutes. That's so annoying!

(Xiao RuO: Tomato ?! Is it a tomato? Microwave Oven, roast tomato ?! =)

 

What do you mean? Flowers? No, it's your heart ~

Reprinted please note, original address: http://blog.csdn.net/musicvs/article/details/8552045

Body:

 

1. Cancel the call of custom update

When we want to stop the update call, we can do this:

void HelloWorld::MutUpdate( float fDelta ){CCLOG("MutUpdate");this->unschedule(schedule_selector(HelloWorld::MutUpdate));}

Unschedule, as its name implies, UN generally indicates cancellation. However, whether we use a parameter or two parameters when registering the UPDATE function, you do not need to transmit other parameters when canceling the function, you only need to pass the function.

(Xiao RuO: I always think it is better to directly read the code as you explain .)

 

When you run the project in debug mode, you will find that the log is printed only once.

2. Cancel the call of default update

It is easy to cancel the default update function call:

this->unscheduleUpdate();

 

Cancellation corresponds to registration. It can be seen from the function name. I don't like it very much.

(Xiao RuO: That's strange !)

 

3. More violent, cancel calling all update functions

Let's try a fun thing: register the two update functions together:

Bool helloworld: Init () {bool Bret = false; do {cc_break_if (! Cclayer: Init (); this-> scheduleupdate ();/* specify the custom function to be executed per frame */This-> schedule (schedule_selector (helloworld :: mutupdate); Bret = true;} while (0); Return Bret ;}

 

We used scheduleupdate and schedule together and tried to run the project in debug mode. It is very interesting. We will see that they all work well:

Update

Mutupdate

Update

Mutupdate

Update

Mutupdate

Update

Mutupdate

What if I want to stop the update call?

(Xiao RuO: Call the cancellation functions respectively !)

 

Well said, but it's too stupid to do this!

(Xiao RuO: Gu ...)

 

We only need a line of code:

this->unscheduleAllSelectors();

 

I don't need to explain it. After this function is called, all update functions will not be called.

4. triggers

Well, I have said so much, and my tomatoes will be grayed out!

(Xiao RuO: Ding! Didn't you say it would last a bit !)

 

Okay. Let's just give it a try. There are not many.

Bool helloworld: Init () {bool Bret = false; do {cc_break_if (! Cclayer: Init ();/* execute the explain function several seconds later */This-> scheduleonce (schedule_selector (helloworld: mutupdate), 5.0f); Bret = true ;} while (0); Return Bret ;}

 

Scheduleonce, as its name implies, is executed once, and the second parameter is the delay time, in seconds. The number of seconds after which the function will be executed and will be executed only once.

OK. Run the project in debug mode. We will see an output in five seconds:

Mutupdate

And there is only one, not many.

 

 

Hello, the timer tutorial is over ~ Let's let it go. Everyone leaves in order.

(Xiao RuO: I won't cooperate with you to get nervous ...)

(Xiao RuO: take the steps one by one. Be careful when taking the steps ...)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.