Game Development Reference
In-Depth Information
That's surprisingly little code to get cocos2d running. As usual, you set the animation
interval, projection, enable Retina mode, and the rest is just connecting the director
with the CCGLView . The latter part simply goes over the list of subviews in the view
controller's view to find one that's subclassed from CCGLView . Once the right view is
found, it's assigned to the director as the director's view .
To enable the fps display via displayStats you'll also have to add all of the
fps_images png files from any cocos2d project to your project via the File
Add Files To … command. Otherwise, the framerate counter won't be displayed.
Next you need to add a cocos2d scene class to the project. Using the File Add Files
to “ViewBasedAppWithCocos2D”… menu item, browse into the cocos2d project you
created earlier and then locate and select both the header and implementation files of
the HelloWorldLayer class. Make sure the Copy items into destination group's
folder (if needed) check box is checked. Alternatively, you can also create a new
cocos2d scene class from a cocos2d file template or manually. You'll be using the
HelloWorldLayer class throughout this example.
Import the HelloWorldLayer class in the ViewController.m file so that you
can run it as your main cocos2d scene:
#import "HelloWorldLayer.h"
All that's left now is to actually start up cocos2d and show and hide its CCGLView .
The switchChanged method in Listing 15-8 contains all the code that's needed to
run and show or stop and hide cocos2d.
Listing 15-8 . Starting and Stopping the cocos2d Scene
- (IBAction)switchChanged:(id)sender
{
UISwitch* switchButton = (UISwitch*)sender;
CCDirectorIOS* director = (CCDirectorIOS*)[CCDirector sharedDirector];
if (switchButton.on)
{
// if there's no running scene yet, add one
if (director.runningScene == nil)
 
 
Search Nedrilad ::




Custom Search