2015-05-18 41 views
0

我有一个项目,我一直在努力了一段时间。我正在使用Sprite-Kit,但现在希望能够从不同于GameScene.swift文件的swift文件中添加子节点。如何从另一个.swift文件添加一个精灵?

有些是从装入GameScene视图控制器主代码如下所示:

let skView:SKView = SKView() 
let theScene:SKScene = SKScene() 

class GameViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if let theScene = 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 */ 
      theScene.scaleMode = .ResizeFill //.AspectFill 
      var customSceneSize = CGSizeMake(2560, 1600) 
      theScene.size = customSceneSize 
      skView.presentScene(theScene) 
      makeFakeStuff() // prepares some sample data 
     } 
    } 

通常我在GameScene.swift文件中使用“self.addChild”精灵添加。这不会从另一个.swift文件工作,所以我曾尝试:

theScene.addChild(flyingCell) 

,我尝试:

skView.scene?.addChild(flyingCell) 

这些指令的执行没有错误,但没有显示在屏幕上。当我尝试以其他方式使用相同的精灵代码并将其放入GameScene.swift文件中并使用“self.addChild”时,它会绘制它,所以精灵代码(未显示)看起来很好(位置,zPosition等)。我还让“flyingCell”成为一个全球变量,看看这是否会有所帮助......不会。有任何想法吗?

+1

子类SKSpriteNode。 – sangony

+0

为什么会有帮助? – Narwhal

+0

因此,您可以在整个项目中使用自定义精灵。 – sangony

回答

1

我的代码中有两件事情是错误的。

首先是我的问题可见:我应该申报“theScene”使用“VAR”,而不是“让”

的第二个问题是在我gamescene,我需要把“theScene =自我”

Apple DTS帮我解决了这个问题。

相关问题