Game Development Reference
In-Depth Information
The UserInterfaceLayer and GameLayer classes both handle touch input, but
independently. To achieve the correct results, you need to use Tar-
getedTouchHandlers , and by using the priority parameter, you can make
sure that the UserInterfaceLayer gets to look at a touch event before the
GameLayer . The UserInterfaceLayer uses the isTouchForMe method to
determine whether it should handle the touch, and it returns YES from the
ccTouchBegan method if it did handle the touch. This keeps other targeted touch
handlers from receiving this touch. Listing 5-8 illustrates the important bits of the
touch event code for the UserInterfaceLayer .
Listing 5-8. Touch Input Processing Using TargetedTouchDelegate
// Register TargetedTouch handler with higher priority than GameLayer
-(void) registerWithTouchDispatcher
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self
// Checks if the touch location was in an area that this layer wants to handle as input.
-(BOOL) isTouchForMe:(CGPoint)touchLocation
CCNode* node = [self getChildByTag:UILayerTagFrameSprite];
return CGRectContainsPoint([node boundingBox], touchLocation);
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
CGPoint location = [MultiLayerScene locationFromTouch:touch];
BOOL isTouchHandled = [self isTouchForMe:location];
if (isTouchHandled)
CCNode* node = [self getChildByTag:UILayerTagFrameSprite];
NSAssert([node isKindOfClass:[CCSprite class]], @"node is not a CCSprite");
// Highlight the UI layer's sprite for the duration of the touch
((CCSprite*)node).color = ccRED;
Search Nedrilad ::

Custom Search