2015-07-20 33 views
1

我有三個不同的.swift文件,GameScene.swift,GameOverScene.swift和StartScene.swift。當我打開我的應用程序時,GameScene顯示出來。我怎樣才能改變它,以便StartScene.swift是第一個顯示的文件?我一直在代碼工作,所以沒有故事板這樣做的方式會很好。謝謝您的幫助!如何更改將以編程方式加載時顯示的swift文件?

編輯: 我覺得我必須改變GameViewController.swift中的某些東西,所以它的代碼如下。

extension SKNode { 
class func unarchiveFromFile(file : String) -> SKNode? { 
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") { 
     var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)! 
     var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData) 

     archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene") 
     let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene 
     archiver.finishDecoding() 
     return scene 
    } else { 
     return nil 
    } 
} 
} 

class GameViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseTimers:"), name:UIApplicationWillResignActiveNotification, object: nil) 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("startTimers:"), name:UIApplicationDidBecomeActiveNotification, object: nil) 


    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { 
     // Configure the view. 
     let skView = self.view as! SKView 
     skView.showsFPS = true 
     skView.showsNodeCount = true 

     /* Sprite Kit applies additional optimizations to improve rendering performance */ 
     skView.ignoresSiblingOrder = true 

     /* Set the scale mode to scale to fit the window */ 
     scene.size = skView.bounds.size 
     scene.scaleMode = .AspectFill 

     skView.presentScene(scene) 
    } 
} 
func pauseTimers(notification : NSNotification) { 
    println("Observer method called") 
    timer.invalidate() 
    scoretimer.invalidate() 
    supertimer.invalidate() 
} 

func startTimers(notification : NSNotification) { 
    println("Observer method called") 
    timerRecreate = true 
    timerz = false 
} 

override func shouldAutorotate() -> Bool { 
    return false 
} 

override func supportedInterfaceOrientations() -> Int { 
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
     return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue) 
    } else { 
     return Int(UIInterfaceOrientationMask.All.rawValue) 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Release any cached data, images, etc that aren't in use. 
} 

override func prefersStatusBarHidden() -> Bool { 
    return true 
} 
} 

我試着改變一切,說遊戲到StartScene,但沒有奏效。

繼承人的文件列表: enter image description here 而我的繼承人應用程序的委託:

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. 
    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:. 
} 


} 

編輯:

我想在下面的後實施第二種方法,但我有三個錯誤。 enter image description here

回答

2

隨着故事板進入您的應用程序委託,尋找這種方法

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject :AnyObject]?) -> Bool { 
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    var storyboard = UIStoryboard(name: "Main", bundle: nil) 

    var initialViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as UIViewController 

    self.window?.rootViewController = initialViewController 
    self.window?.makeKeyAndVisible() 
    } 

如果你不使用故事板

window: UIWindow? 
var initialViewController :StartScene? 
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject :AnyObject]?) -> Bool { 

    initialViewController = StartScene(nibName:"StartScene",bundle:nil) 

    let frame = UIScreen.mainScreen().bounds 
    window = UIWindow(frame: frame) 

    window!.rootViewController = initialViewController 
    window!.makeKeyAndVisible() 
       ... 
} 
+0

所以,如果我想使StartScene.swift的文件加載啓動時,我需要爲StartScene製作一個viewcontroller嗎? – Darkstar

+0

我有Main.Storyboard,但我沒有視圖控制器的StartScene.swift – Darkstar

+0

從我的理解您的問題StartScene將視圖控制器的子類。 'var window:UIWindow?' 'var initialViewController:StartScene?' – Asdrubal

相關問題