问题
I like my code to be explicit so I want to remove the storyboard
file and create SKView
programmatically.
What I have tried so far:
- I created a
SpriteKit
game project in xcode. - Deleted the storyboard file.
- Deleted sroryboard property from
Info.plist
Added this code found in an example to the
AppDelegate
:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; AppViewController *viewController = [[AppViewController alloc] init]; viewController.edgesForExtendedLayout = UIRectEdgeNone; _window.rootViewController = viewController; [_window makeKeyAndVisible]; return YES; }
Added this code to the
AppViewController
:-(void) loadView { self.view = [[SKView alloc] init]; }
The app starts but then crashes on the creation of first sprite with the following warning
EXC_BAD_ACCESS
I think there should be some good examples of how to do this, but I have not found any for the SpriteKit
yet.
回答1:
I fixed the problem by setting a frame to the view:
- (void) loadView
{
self.view = [[SKView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
}
Here is a working example project in Swift
来源:https://stackoverflow.com/questions/29659415/create-skview-programmatically