Game Development Reference
Now you can run the app again, and you'll see the “Hello Cocos2D!” labels being
drawn over the text fields. There's only one issue remaining: the text fields underneath
the cocos2d view won't respond to your touches!
Properly Propagating Touch Events via Hit Testing
The easiest way to have the views behind the cocos2d view respond to touch events is
to completely disable touch input on the cocos2d view. You won't be receiving any
messages from the CCTouchDispatcher anymore if you add this line:
// This will disable all touch events on the cocos2d view
glView.userInteractionEnabled = NO;
Now the text fields behind the cocos2d view will act normally, but touch input for the
cocos2d view is disabled. UIKit views, which are in front of the cocos2d view, should
also work normally and respond to touches, unless you've added them to the cocos2d
glView directly instead of the window.
You may be wondering why disabling touch input on the cocos2d view is the best, or at
least the easiest, option. For that, you have to understand that the cocos2d view is a
UIView that spans the entire screen area. Although you can see through it now that
you've set it up to be transparent, it still responds positively to the UIView hitTest
event. After all, any touch is somewhere on the screen, and because the cocos2d view
is as big as the screen and doesn't take into account what's actually displayed inside its
view, it responds positively to the hit test. So, any touch that reaches the cocos2d view
will be processed by it or the CCTouchDispatcher class. Anything underneath the
cocos2d view is cut off from receiving touch events.
Unfortunately, cocos2d doesn't have a built-in system to forward the hitTest event
to its nodes for them to decide whether they actually need to respond to the touch. I
present you with a solution that uses the node's bounding boxes and requires extending
the CCGLView class with a category.
Caution Only add the following hit test code to the CCGLView class if you
absolutely need it in your project. It will have a negative effect on performance
whenever a touch event is fired, which is basically the whole time the user has