initWithSize在Objective-C中被触发但在Swift中没有

时间:2014-10-01 15:28:28

标签: swift sprite-kit skscene

我有一个非常简单的用Swift编写的SKScene,它没有被触发:

override init(size: CGSize) {
    super.init(size: size)
    backgroundColor = SKColor.whiteColor()
    var bg = SKSpriteNode(imageNamed: "myBackground")
    bg.position = CGPoint(self.size.width/2, self.size.height/2)

    self.addChild(bg)
}

我已经在Objective-C中尝试了相同的代码,并且它在没有问题的情况下被调用,所以我想知道他们是否在Swift中使用initWithSize改变了一些东西,如果我遗漏了一些关键的东西。

注意:SKScene确实被触发,只有此功能没有。

谢谢!

1 个答案:

答案 0 :(得分:0)

In **GameViewcontroller** viewDidLoad(), Make sure your calling like this
   let skView = self.view as SKView

   let scene = **GameScene.init(size:skView.bounds.size)**
   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.scaleMode = .AspectFill

   skView.presentScene(scene)


**GameScene.swift**

**init(size: CGSize)** is parameterized constructor. So you have to call it manually.

override init(size: CGSize) {
 super.init(size: size)
 backgroundColor = SKColor.whiteColor()
 var bg = SKSpriteNode(imageNamed: "myBackground")
 bg.position = CGPoint(x: self.size.width/2, y: self.size.height/2)

 self.addChild(bg)
}

required init?(coder aDecoder: NSCoder) {
 super.init(coder: aDecoder)
}