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