Game Development Reference
In-Depth Information
scheduled selectors incur an additional overhead because they're stored in lists and pri-
oritized against each other.
The method's signature is still the same: it receives a delta time as its only parameter.
But this time the method can be named any way you want, and it's called only every
tenth of a second. This may be useful to check for win conditions if they're so complex
you don't want to run them every frame. Or if you want something to happen after 10
minutes, you could schedule a selector with an interval of 600.
Each selector can only be scheduled once for each object. If you schedule the same se-
lector for the same object a second time, cocos2d will print out a warning telling you
that the selector was already scheduled and its interval has been updated.
Note The @selector(…) syntax may seem weird. It's the Objective-C way
of referring to a specific method by name. The crucial thing here is not to over-
look the colon at the end. It tells Objective-C to look for the method with the
given name and exactly one parameter. If you forget to add the colon at the end,
the program will still compile, but it will crash later. In the Debugger Console,
the error log will read “unrecognized selector sent to instance. . . .”
The number of colons in @selector(. . .) must always match the num-
ber and names of the parameters of the method. For the following method:
-(void) example:(ccTime)delta sender:(id)sender
flag:(bool)aBool the corresponding @selector should be written as
There's one major caveat with scheduling your own selectors, and with the @select-
or(. . .) keyword in general. By default, the compiler does not complain at allif
the method's name doesn't exist. Instead, your app will simply crash with an “unrecog-
nized selector sent to instance” error . Because the message is sent by cocos2d intern-
ally, you'll find it hard to figure out the cause of the problem. Luckily, there's a com-
piler warning you can enable. Figure 3-6 shows the “Undeclared Selector” warning en-
abled for the NodeHierarchy project, and the Essentials Xcode project for this
chapter has it enabled as well.
Search Nedrilad ::

Custom Search