Cocos2d 2.0 with Storyboard example
The environment I am using for this example is:- Xcode 4.3
- cocos2d for iPhone 2.0 rc2
Step 1. Create a new Xcode project. Choose a template for your new project. Select [iOS/application/Single View Application]. Click [Next] Button.
Choose a template for your new project |
Step 2. Choose options for your new project. Enter your Product Name, Company Identifier, and Class Prefix. Check [Use Storyboards]. Click [Next] Button. In the next window select the directory and click [Create] Button.
Choose options for your new project |
Step 3. New project created. Now, you got the start point for Cocos2d with Storyboard project.
New project created |
Step 4. Open Finder and find your project files Your initial project files in the Finder looks like this:
Initial Project Files |
Step 5. Create libs directory under your project directory and copy libcocos2d.a and libCocosDenshion.a create from cocos2de-iphone-2.0 project into libs directory. Your project files in the Finder looks like this:
Project Files include libcocos2d.a and libCocosDenshion.a |
Step 6. In the project window. Click [Build Phases/Link Binary with Libraries]. Click [+](Add items) Button. Under [Choose frameworks and libraries to add:] window. Select [AudioToolbox.framework, OpenAL.framework, OpenGLES.framework, and QuartzCore.framework]. Click [Add] Button.
Add AudioToolbox, OpenAL, OpenGLES, and QuartzCore Framework |
Step 7. Click [+](Add items) Button again. Under [Choose frameworks and libraries to add:] window. Click [Add Others...] button. Go to libs directory under our newly create project. Select libcocos2d.a and libCocosDenshion.a.
Select libcocos2d.a and libCocosDenshion.a |
Step 8. Click [Open] Button. Library libcocos2d.a and libCocosDenshion.a also added to the project.
Move newly add Frameworks and Libraries to Frameworks group |
Step 10. Click your project icon again and choose [Build Settings]. Select [Other Linker Flags] and add value [-lz].
Set Other Linker Flags to -lz |
Step 11. Set [Always Search User Paths] to Yes. Set [Library Search Paths] to your libs directory. Be remember to check libs path. Set [User Header Search Paths] to cocos2d-iphone-2.0 project path. Be remember to check project path.
Set Header and Library Search Paths |
Step 12. Click your project icon again and choose [Summary]. Uncheck [Portrait].
Uncheck Portrait |
Step 13. Click [MainStoryboard.storyboard]
Storyboard |
Step 14. Click [iOS View Controller] inside the Storyboard. Select menu [Editor/Embed in/Navigation Controller]
Embed in Navigation Controller |
Step 15. Add files with content as follows and your project should work
HelloWorldLayer.h
//
// HelloWorldLayer.h
//
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
HelloWorldLayer.m
//
// HelloWorldLayer.m
//
// Import the interfaces
#import "HelloWorldLayer.h"
#pragma mark - HelloWorldLayer
// HelloWorldLayer implementation
@implementation HelloWorldLayer
// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
}
return self;
}
@end
iOSAppDelegate.h
//
// iOSAppDelegate.h
//
#import <UIKit/UIKit.h>
@interface iOSAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) IBOutlet UINavigationController *navigationController;
@end
iOSAppDelegate.m
//
// iOSAppDelegate.m
//
#import "iOSAppDelegate.h"
@implementation iOSAppDelegate
@synthesize window = _window;
@synthesize navigationController = _navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
iOSViewController.h
// iOSViewController.h
//
#import "cocos2d.h"
@interface iOSViewController : UIViewController <CCDirectorDelegate>
- (CCGLView *)createDirectorGLView;
- (void)didInitializeDirector;
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)applicationWillResignActive:(NSNotification *)notification;
- (void)applicationDidBecomeActive:(NSNotification *)notification;
- (void)applicationDidEnterBackground:(NSNotification *)notification;
- (void)applicationWillEnterForeground:(NSNotification *)notification;
- (void)applicationWillTerminate:(NSNotification *)notification;
- (void)applicationSignificantTimeChange:(NSNotification *)notification;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
@end
iOSViewController.m
//
// iOSViewController.m
//
#import "iOSViewController.h"
#import "HelloWorldLayer.h"
@implementation iOSViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CCDirector *director = [CCDirector sharedDirector];
if ([director isViewLoaded] == NO) {
director.view = [self createDirectorGLView];
[self didInitializeDirector];
}
[director setWantsFullScreenLayout:YES];
[director setDelegate:self];
[self addChildViewController:director];
[self.view addSubview:director.view];
[self.view sendSubviewToBack:director.view];
[self.view setMultipleTouchEnabled:YES];
[director didMoveToParentViewController:self];
[director setProjection:kCCDirectorProjection2D];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:director];
[navigationController setNavigationBarHidden:YES];
if (director.runningScene)
[director replaceScene:[HelloWorldLayer scene]];
else
[director runWithScene:[HelloWorldLayer scene]];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[self.navigationController setNavigationBarHidden:YES animated:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationSignificantTimeChange:) name:UIApplicationSignificantTimeChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
[self.navigationController setNavigationBarHidden:NO animated:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidUnload {
[super viewDidUnload];
[[CCDirector sharedDirector] setDelegate:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[[CCDirector sharedDirector] purgeCachedData];
}
- (CCGLView *)createDirectorGLView {
CCGLView *glView = [CCGLView viewWithFrame:[[[UIApplication sharedApplication] keyWindow] bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO numberOfSamples:0];
return glView;
}
- (void)didInitializeDirector {
CCDirector *director = [CCDirector sharedDirector];
[director setAnimationInterval:1.0f/60.0f];
[director enableRetinaDisplay:YES];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
[[CCDirector sharedDirector] resume];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification {
[[CCDirector sharedDirector] stopAnimation];
}
- (void)applicationWillEnterForeground:(NSNotification *)notification {
[[CCDirector sharedDirector] startAnimation];
}
- (void)applicationWillTerminate:(NSNotification *)notification {
[[CCDirector sharedDirector] end];
}
- (void)applicationSignificantTimeChange:(NSNotification *)notification {
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}
//- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// [self viewDidLoad];
//}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
// return toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;
// return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
return NO;
}
@end
沒有留言:
張貼留言