没有故事板的SpriteKit示例

3
我希望写一个使用Swift和SpritKit的iOS游戏示例,该示例仅提供Xcode代码。这意味着我不想使用GameScene.sks、actions.sks和main.storyboard。我知道如何在没有storyboard的情况下编写它,但是我无法在没有.sks文件的情况下使其正常工作。您能否告诉我必须更改什么或者是否可以提供完整的项目?
1个回答

4

首先,您需要拥有一个 View Controller。您可以根据自己的喜好调整属性。这是我的:

import UIKit
import SpriteKit


class GameViewController: UIViewController {

    // MARK: View Controller overrides
    override func viewDidLoad() {
        super.viewDidLoad()

        view = SKView(frame: view.bounds)

        if let view = self.view as! SKView? {
            // Initialise the scene
            let scene = GameScene(size: view.bounds.size) // <-- IMPORTANT: Initialise your first scene (as you have no .sks)

            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)

            // Scene properties
            view.showsPhysics = false
            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true
        }
    }

}

接下来,您需要创建一个与首个场景相对应的类。我的类名为GameScene,在视图控制器中被初始化。请确保这是SKScene子类。代码如下:

import SpriteKit

class GameScene: SKScene {

    /* All Scene logic (which you could extend to multiple files) */

}

如果您有任何问题,请告诉我 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接