Game Development Reference
In-Depth Information
Listing 8-24 . Switching to the page with the highest unlocked level
-(void) didLoadFromCCB
{
// Code in Listing 8-23 omitted ...
id levelSelect = _levelSelectScrollView.contentNode;
_levelSelectScrollView.delegate = levelSelect;
}
This takes the Scroll View 's Content node and assigns it as the Scroll View 's delegate
instance. The delegate object receives the CCScrollViewDelegate methods. The
use of id here is important because if you didn't use it, the compiler would warn that you
are trying to assign an incompatible type, unless you were to cast it to an object conform-
ing to said protocol: ( id<CCScrollViewDelegate> ).
Note that you could have assigned the delegate just as well in the
MainMenuLevelSelect class. For instance, the following would work in the
didLoadFromCCB method of MainMenuLevelSelect.m :
CCScrollView* scrollView = (CCScrollView*)self.parent;
scrollView.delegate = self;
It amounts to the same code, and I leave it up to you which you should use. But it serves
to illustrate there's almost always more than one way to do the same thing.
Next, open MainMenuLevelSelect.h to make the class conform to the
CCScrollViewDelegate protocol, as highlighted in Listing 8-25 .
Listing 8-25 . Conforming to the CCScrollViewDelegate protocol
#import "CCNode.h"
@class MainMenuButtons;
@interface MainMenuLevelSelect : CCNode
<CCScrollViewDelegate>
@property (weak) MainMenuButtons* mainMenuButtons;
@end
The protocol defines the following methods as optional, so you don't have to implement
all (or any) of them. I added comments to Listing 8-26 to explain when each method runs.
Search WWH ::




Custom Search