【问题标题】:How do I get xcode to run the code in GameScene.swift instead of GameScene.sks [closed]如何让 xcode 在 GameScene.swift 而不是 GameScene.sks 中运行代码 [关闭]
【发布时间】:2016-12-18 08:38:23
【问题描述】:

如何让 xcode 在 GameScene.swift 而不是 GameScene.sks 中运行代码?

【问题讨论】:

  • 你在创建精灵节点时遇到了什么问题?
  • 要创建场景而不是从 sks 文件中加载,你应该按照 Mina 所说的去做。尽管如此,即使您从 sks 文件加载场景,您也可以通过编程方式添加节点,例如在 didMove(toView:) 方法中或在任何引用场景的自定义方法中,或在触摸处理方法中。所以我不明白这里有什么问题......

标签: swift sprite-kit swift3 xcode8


【解决方案1】:

一切都在GameViewController

if let view = self.view as! SKView? {
    // Load the SKScene from 'GameScene.sks'
    if let scene = SKScene(fileNamed: "GameScene") {
        // Set the scale mode to scale to fit the window
        scene.scaleMode = .aspectFill

        // Present the scene
        view.presentScene(scene)
    }

     view.ignoresSiblingOrder = true

     view.showsFPS = true
     view.showsNodeCount = true
 }

你可以用这个代码替换它

if let view = self.view as! SKView? {
    let scene = GameScene(size: view.bounds.size)

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

    // Present the scene
    view.presentScene(scene)

    view.ignoresSiblingOrder = true

    view.showsFPS = true
    view.showsNodeCount = true
}

它将运行GameScene.swift 中的代码,而不是GameScene.sks 中的代码。所以你可以通过代码创建节点。

【讨论】:

  • 非常感谢 Mina,我终于设法让 xcode 运行我的 GameScene.swift 文件!再次非常感谢您。
  • 我遇到了同样的问题。开始一个新项目并选择一个带有破折号的名称解决了这个问题。
  • @Mina 这对我不起作用。我在 GameScene.swift 中添加的节点在运行项目时不显示,我只是得到一个深灰色的屏幕。关于如何解决这个问题的任何想法?
猜你喜欢
  • 1970-01-01
  • 2017-10-07
  • 2016-08-21
  • 1970-01-01
  • 2015-01-02
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
相关资源
最近更新 更多