Game Development Reference
In-Depth Information
gramming tasks easier. In layman's terms, block objects are C callback functions that
can be created within other functions, assigned to variables for later use, passed on to
other functions, and run asynchronously at a later time. Because a block object has read
access to the local variables of the function or scope it was defined in, it typically re-
quires fewer arguments than a regular callback method. Optionally, with the __block
storage specifier, you can also allow the block object to modify variables in its enclos-
ing scope.
Tip Refer to Apple's Blocks Programming Topics documentation if you're in-
terested in more details about block objects: ht-
tp://developer.apple.com/library/mac/#documentation/
Cocoa/Conceptual/Blocks/Articles/00_Introduction.html .
You'll also find more examples near the end of Chapter 3 where I explain how
to use blocks with menus, specifically the CCMenuItem classes.
I'll cut out the actual block object from Listing 14-5 to discuss it separately:
^(NSError* error)
{
[self setLastError:error];
}
It looks like a method, except it has no name and it begins with a caret symbol ( ^ ). The
NSError pointer is the only variable passed to it, but commas can delimit multiple
variables, as in this example:
^(NSArray* scores, NSError* error)
{
[self setLastError:error];
[delegate onScoresReceived:scores];
}
If that reminds you of a C method's parameters, you're correct. If you want, you can
consider a block to be a C method whose name is ^ and that can be passed to one of
the many Game Kit methods taking block objects as parameters.
Search Nedrilad ::




Custom Search