swiftにおいて、以下のサイトを参考にSKViewを設定しました。
参考サイト
ここで見ていただきたいのは以下に記載したコードのviewWillAppear関数です。
AppDelegateでrootViewControllerをNavigationControllerに変更したのですが、そうすると
let skView = self.view as! SKView
の行でThread 1: SGBRTのエラーが発生します。
NavigationControllerのままこのエラーを解決する方法はありますか?
ここで、GameSceneクラスはSKSceneを継承したクラスです。
swift
1import UIKit 2import SpriteKit 3 4class ViewController: UIViewController { 5 6 override func viewWillAppear(animated: Bool) { 7 let skView = self.view as! SKView 8 9 let scene = GameScene(size: skView.bounds.size) 10 11 skView.presentScene(scene) 12 } 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23} 24 25
追記
AppDelegate.swiftコードの追加
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. //cconfigure ViewController class instance. let viewController: ViewController = ViewController() //configure NavigationController class let myNavigationController = UINavigationController(rootViewController: viewController) //set myNavigationController to rootViewController self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.rootViewController = myNavigationController self.window?.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { // 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. } func applicationDidEnterBackground(application: UIApplication) { // 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. } func applicationWillEnterForeground(application: UIApplication) { // 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. } func applicationDidBecomeActive(application: UIApplication) { // 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. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }
エラーの発生原因については特定出来ているのでしょうか。
self.view as! SKView キャストしているコードですが、self.view は間違いなくSKView型なのでしょうか。
もう少し確認されたことも含めて記載したほうが良いですよ。
原因がわからないので質問しているのですが?
とりあえず他に原因究明に役立ちそうなものとしてエラー発生とともに
「Could not cast value of type 'UIView' (0x109724b20) to 'SKView' (0x10897ead0).」という文もデバッグエリアに表示されました。
rootViewControllerを変更している部分も掲載していただけますでしょうか?
YasuhiroMiyakeさんが指摘しているのは、self.viewがSKView型となっているかどうかです。プログラムやエラーメッセージを見る限り、self.viewはSKView(またはそのサブクラス)ではありません。NavigationViewが必要でしたら、NavigationViewの中にSKViewを追加してみてはどうでしょうか。
AOKINAOさん、補足頂きありがとうございます。
ここは大人の対応? ということでスルーしたいと思います。
何かを書くと説教ぽくなってしまいますので。

回答2件
あなたの回答
tips
プレビュー