GameScene.sks和GameScene.swift

时间:2017-05-22 23:49:51

标签: ios swift xcode sprite-kit

遇到GameScene.sks和GameScene.swift问题。我试图为我的项目添加标题屏幕,然后从那里开始下坡。标题屏幕工作正常,但问题是当我编辑Gameviewcontroller.swift时。

This is how I want it to look, mind you I deleted the gamescene.sks at the start of the project but I realized you need sks file when you switch between scenes

This is how it ends up looking when I re-add the gamescene.sks

在添加TitleScreen

之前,这是GameViewController.Swift到工作文件
import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = GameScene(size: view.bounds.size)
        let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .resizeFill
        skView.presentScene(scene)
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

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

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

这是在更改视图以首先加载标题屏幕之后:

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

         if let scene = titleScreen(fileNamed: "titleScreen") {

                //Configure view 
                let skView = self.view as! SKView
                skView.showsFPS = true
                skView.showsNodeCount = true
                skView.ignoresSiblingOrder = true
                // Set the scale mode to scale to fit the window
               // scene.scaleMode = .aspectFill

                scene.size = self.view.frame.size
                // Present the scene
                skView.presentScene(scene)


            }


    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

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

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您可能没有配置GameScene实例......

scene.scaleMode = .resizeFill只在你的gameviewcontroller中调用...是吗?

这意味着只有你的第一个场景被配置。

在标题场景中,只要您是presentScene,就需要配置下一个场景......

func configureScene(_ scene: SKScene) -> SKScene {
  scene.scaleMode = .resizeFill
  scene.anchorPoint = CGPoint(x: 0.5, y: 0.5) // Or whatever you want..
  scene.size = UIWindow().frame.size // Or whatever size you want.
  return scene
}

实施:

// Where self is an SKScene...
self.presentScene(configureScene(desiredScene))

同样如WW所说,帧和场景大小不同,会影响一切的外观。