Game Development Reference
Of course, behind the scenes, all UIKit views are also rendered by OpenGL ES; there's
just a lot more stuff going behind the scenes that's needed for graphical user interfaces
but is essentially a waste of performance if you want to make games. You may remem-
ber the very early games that were written entirely with UIKit, Core Graphics, and
Core Animation? If not, good for you. They were often slow and unresponsive.
One immediately noticeable difference between Cocoa Touch and cocos2d is the co-
ordinate system. Cocos2d has the origin point (0,0) at the lower left-hand corner of the
screen, whereas UIKit views have their origin point at the upper left-hand corner. You
need to consider the differences in coordinate systems used by UIKit and OpenGL ES
when positioning but also when manually rotating UIKit views.
And because cocos2d is programmed to interact directly with the graphics hardware, it
uses its own hierarchy of displaying graphical elements. In cocos2d that's the CCNode
hierarchy where you can add any CCNode -based class to any other CCNode , with a
CCScene as the very first element in that hierarchy. The UIKit framework, on the oth-
er hand, operates with a view hierarchy where you add UIView -based classes to an-
other, often with a UIWindow as the topmost element. Both view hierarchies are in-
compatible, so you can't add a UIView to a CCNode , and vice versa. This is notice-
able when you change from one CCScene to another using a CCTransitionS-
cene . While the cocos2d nodes all move aside, the UIKit views remains fixed in place
unless you also move them separately and in sync with the cocos2d animation. It's ac-
tually a good idea to avoid this kind of situation in the first place.
Alert: Your First UIKit View in cocos2d
The simplest and most straightforward example for using a UIKit view with cocos2d is
found in the example project CocosWithCocoa01. It displays a UIAlertView on top
of the cocos2d scene created from the default cocos2d project template. To re-create
the project from scratch, open Xcode and go to File New New Project to bring
up the New Project dialog. In that dialog, select cocos2d under the iOS list and create
the cocos2d project.
Let's modify the HelloWorldLayer class to display a UIAlertView . The inter-
face in HelloWorldLayer.h needs only one small addition; namely, the Hel-
loWorldLayer class needs to support the UIAlertViewDelegate protocol: