Game Development Reference
You should also be aware that any UIKit view can only be either in front of the entire
cocos2d view or entirely behind it. You can't have a UIKit view that's in front of some
of the cocos2d scene's sprites, labels, effects, and so on, while at the same time being
behind other cocos2d sprites, labels, effects, or other nodes. In other words, you can't
“sandwich” a UIKit view between two or more cocos2d nodes.
You can do the opposite, however, although with some limitations and manual labor.
You can “sandwich” the cocos2d view: UIKit views in the background, then a transpar-
ent cocos2d view with some nodes, and then some more UIKit views in the fore-
ground. This approach requires a little more work setting up the view hierarchy and
making the cocos2d view transparent. Imagine playing a full-motion video in the back-
ground, over which you draw cocos2d sprites, and the rest of the user interface is made
up of UIKit views.
But touch input remains a problem: either the UIKit views and not the cocos2d view
will receive input, or those UIKit views added to the cocos2d view and the cocos2d
view itself will receive input but not the views in the background. This has to do with
the fact that the cocos2d view receives all touches on the screen simply because it oc-
cupies the entire screen. So, you need to write additional code to process the touches on
the cocos2d view and then decide whether the cocos2d view should forward the
touches—for example, if the user didn't touch any of the cocos2d sprites currently dis-
played on screen.
Allowing all views to receive input is possible, and I'll provide you with a basic solu-
tion later in this chapter. But it's up to you to improve and adapt it for your own needs.
Depending on your needs, the necessary code changes may actually be substantial and
challenging in order to fully support UIKit views both in front of the cocos2d view and
behind it and have all views reacting properly to touch input.
How Is Cocoa Touch Different from cocos2d?
Let's take a look at the major differences of Cocoa Touch programming compared to
working with cocos2d. One difference is the Model-View-Controller pattern common
to Cocoa Touch applications but essentially missing from cocos2d. And then you also
have to consider the differences caused by cocos2d's OpenGL ES view because it be-
haves differently in some aspects than a regular UIView .