Game Development Reference
In-Depth Information
// unschedule the current method by using the _cmd keyword
[self unschedule:_cmd];
The hidden variable _cmd is available in all Objective-C methods. It's the selector of
the current method. In the previous code example, _cmd is equivalent to writing @se-
lector(tenMinutesElapsed:) . Unscheduling _cmd effectively stops the
tenMinutesElapsed method from ever being called again. You can also use _cmd
for scheduling the selector in the first place if you want the current method to be res-
cheduled. Let's assume you need a method called at varying intervals, and this interval
is changed each time the method is called. In this case, your code can make use of
_cmd like this:
-(void) scheduleUpdates
// schedule the first update as usual
[self schedule:@selector(irregularUpdate:) interval:1];
-(void) irregularUpdate:(ccTime)delta
// unschedule the method first so cocos2d won't complain
[self unschedule:_cmd];
// I assume you'd have some kind of logic other than random to determine
// the next time the method should be called
float nextUpdate = CCRANDOM_0_1() * 10;
// then re-schedule it with the new interval using _cmd as the selector
[self schedule:_cmd interval:nextUpdate];
Using the _cmd keyword will save you a lot of pain in the long run because it avoids
the dreaded issue of scheduling or unscheduling the wrong selector, and it decouples
the code from having to know the name of the method it's used in.
I'll mention one final scheduling issue, and that's prioritizing updates. Take a look at
the following code:
Search Nedrilad ::

Custom Search