iOS Development Oc-nstimer Timer simple to use

Source: Internet
Author: User

In the game development, many times will use a certain time to automatically do what the effect, such as every second automatically add an element. Obviously, I've used it. This effect can be achieved with Nstimer in Object-c.

1. Definition Nstimer

@property (nonatomic) Nstimer *mousetimer; // Timer

--------------------------------------------------------------------------------------------------------------

2. Initialize, and specify what it does

_mousetimer=[nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (MouseMove) Userinfo:nil Repeats:yes];

As soon as we run to this sentence, he begins the cycle of execution, knowing that we let it stop, here is an approximate explanation of the parameters:

Scheduledtimerwithtimeinterval, specify how often to perform an action;

Target, which specifies the target of the response to the result of this timer;

Selector, specifies the action that the timer performs each time, here MouseMove is a function that I write myself;

UserInfo, transmission of the things sent, parameters sent ;

Repeats, set whether the timer calls the target method repeatedly, that is, MouseMove.

--------------------------------------------------------------------------------------------------------------

3. Turn off the timer

You need to stop or turn off the timer when you want to stop it, such as when the game is over and leave the game screen.

Stop: (PS: That's why I'm positioning it as a global variable because it can be manipulated anywhere on this page)

[_mousetimer setfiredate:[nsdate distantfuture];

Go on:

[_mousetimer setfiredate:[nsdate Date]];

Shut down: (Ps:invalidate is simply shutting down the timer and not releasing resources.) So we have to release the resources ourselves. )

[_mousetimer invalidate]; // Turn off timer _mousetimer = nil; // Freeing Resources

In this way, you can control some of the effects of the game very well.

iOS Development Oc-nstimer Timer simple to use

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.