Create SKView programmatically?

谁都会走 提交于 2021-02-19 08:05:06

问题


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:

  1. I created a SpriteKit game project in xcode.
  2. Deleted the storyboard file.
  3. Deleted sroryboard property from Info.plist
  4. 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;
    }
    
  5. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!