Game Development Reference
In-Depth Information
Figure 3-9 . This is the menu produced by the code in Listing 3-1
Menu Items with Blocks
Instead of specifying a target and selector, menu items can also use blocks. What now?
A block is like a C function but with the important distinction that the block code can
access variables in the scope where the block is declared. You can write a block inside
other functions, store it in variables, and pass it as a parameter. Blocks also have access
to variables in the scope they're declared in. This makes blocks a very powerful
concept, but underused because the syntax is slightly confusing. Apparently Apple also
recognized that because they changed the title of the Blocks Programming Guide to A
Short Practical Guide to Blocks, which you'll find here: ht-
tp://developer.apple.com/library/ios/#featuredarticles/
Short_Practical_Guide_Blocks/_index.html .
Without going into too much theory, blocks are best explained by example. Let's see
how a menu item using a block looks:
NSArray* items = [NSArray arrayWithObjects:toggleBlockOn, toggleBlockOff, nil];
CCMenuItemToggle* item4 = [CCMenuItemToggle itemWithItems:items
block:^(id sender) {
// sender is the CCMenuItemToggle
CCMenuItemToggle* toggleItem = (CCMenuItemToggle*)sender;
int index = toggleItem.selectedIndex;
 
Search Nedrilad ::




Custom Search