我有一个用 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 会被触发,只有这个函数不会。
谢谢!
最佳答案
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)
}
关于swift - initWithSize 在 Objective-C 中触发,但在 Swift 中不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26144912/