This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:creating_a_sparrow_project_from_scratch [2014/07/03 16:55] – [Create a new Xcode Project] daniel | tutorials:creating_a_sparrow_project_from_scratch [2014/07/07 13:10] (current) – [Swift] daniel | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== How to create a Sparrow Project from Scratch ====== | ||
+ | |||
+ | This tutorials shows you how to get from a brand-new, empty Xcode project to a Sparrow-powered scaffold. It requires at least Sparrow 2.1 and Xcode 5. | ||
+ | |||
+ | ===== Create a new Xcode Project ===== | ||
+ | |||
+ | Via "File - New - Project", | ||
+ | |||
+ | Beginning with Xcode 6, you can use either **Objective-C** or **Swift** as programming language for your Game. Sparrow supports both. The set-up procedure is almost the same; this guide will help you with both languages. | ||
+ | |||
+ | ===== Add the Sparrow Framework ===== | ||
+ | |||
+ | Now we are ready to add Sparrow to our project. Beginning with Sparrow 2.1, there are two different ways to do that. | ||
+ | |||
+ | <note warning> | ||
+ | |||
+ | ==== Via " | ||
+ | |||
+ | Beginning with Sparrow 2.1, a " | ||
+ | |||
+ | In your new project, first click on the project name in the " | ||
+ | |||
+ | <note tip> | ||
+ | The advantage of this solution is that it's very easy to set-up. The downside: you can't peek into / modify the Sparrow source while you're working on your game. | ||
+ | </ | ||
+ | |||
+ | ==== Via " | ||
+ | |||
+ | - If this is your first Sparrow project, set up the " | ||
+ | - In the " | ||
+ | * Copy items (...): no | ||
+ | * Folders: Create Groups for any added folders | ||
+ | * Add to Target: (choose your target) | ||
+ | - Click on the Sparrow project in the " | ||
+ | - In the " | ||
+ | |||
+ | <note tip> | ||
+ | This solution is a little more work to set-up; in exchange, you can easily look into the Sparrow source anytime. | ||
+ | </ | ||
+ | |||
+ | ===== Add required Frameworks ===== | ||
+ | |||
+ | Click on the project name in the navigator on the left to show the project settings. Click on the "Build Phases" | ||
+ | |||
+ | * AudioToolbox.framework | ||
+ | * AVFoundation.framework | ||
+ | * GLKit.framework | ||
+ | * OpenAL.framework | ||
+ | * OpenGLES.framework | ||
+ | * QuartzCore.framework | ||
+ | * libz.dylib | ||
+ | * libSparrow.a | ||
+ | |||
+ | ===== Modify the Source Code ===== | ||
+ | |||
+ | ==== Objective-C ==== | ||
+ | |||
+ | In " | ||
+ | |||
+ | <code objc> | ||
+ | #ifdef __OBJC__ | ||
+ | #import < | ||
+ | #import < | ||
+ | #import < | ||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | Then create a new class called " | ||
+ | |||
+ | <code objc Game.h> | ||
+ | @interface Game : SPSprite | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | <code objc Game.m> | ||
+ | #import " | ||
+ | |||
+ | @implementation Game | ||
+ | |||
+ | - (id)init | ||
+ | { | ||
+ | if ((self = [super init])) | ||
+ | { | ||
+ | // This is where the code of your game will start; | ||
+ | // in this sample, we add just a simple quad to see if it works. | ||
+ | | ||
+ | SPQuad *quad = [SPQuad quadWithWidth: | ||
+ | quad.color = 0xff0000; | ||
+ | quad.x = 50; | ||
+ | quad.y = 50; | ||
+ | [self addChild: | ||
+ | } | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | In " | ||
+ | |||
+ | <code objc> | ||
+ | #import " | ||
+ | |||
+ | @implementation AppDelegate | ||
+ | { | ||
+ | SPViewController *_viewController; | ||
+ | UIWindow *_window; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Furthermore, | ||
+ | |||
+ | <code objc> | ||
+ | - (BOOL)application: | ||
+ | didFinishLaunchingWithOptions: | ||
+ | { | ||
+ | CGRect screenBounds = [UIScreen mainScreen].bounds; | ||
+ | _window = [[UIWindow alloc] initWithFrame: | ||
+ | | ||
+ | _viewController = [[SPViewController alloc] init]; | ||
+ | | ||
+ | // Enable some common settings here: | ||
+ | // | ||
+ | // _viewController.showStats = YES; | ||
+ | // _viewController.multitouchEnabled = YES; | ||
+ | // _viewController.preferredFramesPerSecond = 60; | ||
+ | | ||
+ | [_viewController startWithRoot: | ||
+ | | ||
+ | [_window setRootViewController: | ||
+ | [_window makeKeyAndVisible]; | ||
+ | | ||
+ | return YES; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Swift ==== | ||
+ | |||
+ | In order to be able to access Sparrow from Swift, we first need to set up a so-called " | ||
+ | |||
+ | <code objc Bridging-Header.h> | ||
+ | // Use this file to import those Objective-C files that you would like to expose to Swift. | ||
+ | |||
+ | #import < | ||
+ | </ | ||
+ | |||
+ | Xcode needs to find this header, of course. In the "Build Settings" | ||
+ | |||
+ | $(PROJECT_NAME)/ | ||
+ | |||
+ | //(The actual path depends on your project setup.)// | ||
+ | |||
+ | With this out of the way, we can now use all Sparrow classes from Swift. Let's start with our root Sparrow class, called " | ||
+ | |||
+ | <code actionscript Game.swift> | ||
+ | import Foundation | ||
+ | |||
+ | class Game : SPSprite { | ||
+ | |||
+ | init() { | ||
+ | super.init() | ||
+ | |||
+ | var quad = SPQuad(width: | ||
+ | quad.x = 50 | ||
+ | quad.y = 50 | ||
+ | addChild(quad) | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | Now we just have to make sure that Sparrow starts up! To do that, modify " | ||
+ | |||
+ | <code actionscript AppDelegate.swift> | ||
+ | import UIKit | ||
+ | |||
+ | @UIApplicationMain | ||
+ | class AppDelegate: | ||
+ | | ||
+ | var _window: UIWindow! | ||
+ | var _viewController: | ||
+ | |||
+ | func application(application: | ||
+ | |||
+ | _viewController = SPViewController() | ||
+ | _viewController.showStats = true | ||
+ | _viewController.multitouchEnabled = true | ||
+ | _viewController.preferredFramesPerSecond = 60 | ||
+ | _viewController.startWithRoot(Game.self, | ||
+ | |||
+ | _window = UIWindow(frame: | ||
+ | _window.rootViewController = _viewController | ||
+ | _window.makeKeyAndVisible() | ||
+ | |||
+ | return true | ||
+ | } | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <note info> | ||
+ | My Beta-Version of Xcode did not find Sparrow at this point — but the errors disappeared after I cleaned the project ('' | ||
+ | |||
+ | Still, I received the following linker warning: | ||
+ | |||
+ | directory not found for option | ||
+ | ' | ||
+ | |||
+ | If the same happens to you, remove that path from the " | ||
+ | </ | ||
+ | |||
+ | ===== Phew! Done! (Hopefully) ===== | ||
+ | |||
+ | If everything works, you should be able to launch the new project. You'll see the iPhone simulator with a red square on its screen. Congratulations! | ||