我在“fatalError("init(coder:) has not been implemented")”处收到错误
Posted
技术标签:
【中文标题】我在“fatalError("init(coder:) has not been implemented")”处收到错误【英文标题】:I get a error at "fatalError("init(coder:) has not been implemented")" 【发布时间】:2017-06-22 11:49:04 【问题描述】:我找不到解决问题的方法,也许我没有正确实施。当我运行它时,我只是得到一个空白屏幕。我尝试了在此站点上看到的不同方法,但它们都不起作用。该错误在“fatalError("init(coder:) has not been implemented")”上突出显示。我有什么明显的遗漏吗?
import SpriteKit
import GameplayKit
import CoreMotion
class GameScene: SKScene
let gameArea: CGRect
override init(size: CGSize)
let maxAspectRatio: CGFloat = 16.0/9.0
let playableWidth = size.height / maxAspectRatio
let margin = (size.width - playableWidth) / 2
gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)
super.init(size: size)
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
var Red = SKSpriteNode(imageNamed: "red")
var motionManager = CMMotionManager()
var destX:CGFloat = 0.0
override func didMove(to view: SKView)
let background = SKSpriteNode(imageNamed: "stars")
background.size = self.size
background.zPosition = 0
//background.setScale(1)
background.position = CGPoint(x: 0, y: 0)
self.addChild(background)
Red.setScale(0.25)
Red.position = CGPoint(x: 0 , y: -500)
self.addChild(Red)
Red.zPosition = 2
if motionManager.isAccelerometerAvailable == true
motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler:
data, error in
let currentX = self.Red.position.x
self.destX = currentX + CGFloat((data?.acceleration.x)! * 100)
)
if Red.position.x > gameArea.maxX
Red.position.x = gameArea.maxX
if Red.position.x < gameArea.maxX
Red.position.x = gameArea.maxX
override func update(_ currentTime: CFTimeInterval)
let action = SKAction.moveTo(x: destX, duration: 1)
self.Red.run(action)
func firebullet ()
let bullet = SKSpriteNode(imageNamed: "gul")
bullet.setScale(0.1)
bullet.position = Red.position
bullet.zPosition = 1
self.addChild(bullet)
let moveBullet = SKAction.moveTo(y: self.size.height + bullet.self.size.height, duration: 1)
let deleteBullet = SKAction.removeFromParent()
let Bulletsequence = SKAction.sequence([moveBullet, deleteBullet])
bullet.run(Bulletsequence)
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?)
firebullet ()
【问题讨论】:
fatal error: init(coder:) has not been implemented error despite being implemented的可能重复 我该如何解决? 【参考方案1】:required init?(coder aDecoder: NSCoder)
super.init(coder: aDecoder)
代替
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
【讨论】:
【参考方案2】:我刚刚在 Playground 中测试了您的代码并添加了一些内容,并且运行良好。签出this。
import SpriteKit
import PlaygroundSupport
class GameScene: SKScene
let gameArea: CGRect
override init(size: CGSize)
let maxAspectRatio: CGFloat = 16.0/9.0
let playableWidth = size.height / maxAspectRatio
let margin = (size.width - playableWidth) / 2
gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)
super.init(size: size)
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
// Create the SpriteKit View
let view = SKView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
// Create the scene and add it to the view
let scene = GameScene(size: CGSize(width: 500, height: 500))
scene.scaleMode = SKSceneScaleMode.aspectFit
scene.backgroundColor = .white
view.presentScene(scene)
// Add a red box to the scene
let redBox = SKSpriteNode(color: SKColor.red, size: CGSize(width: 200, height: 200))
redBox.position = CGPoint(x: 250, y: 250)
redBox.run(SKAction.repeatForever(SKAction.rotate(byAngle: -5, duration: 5)))
scene.addChild(redBox)
// Show in assistant editor
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
【讨论】:
再看一下代码,我添加了更多,所以它不会脱离上下文。以上是关于我在“fatalError("init(coder:) has not been implemented")”处收到错误的主要内容,如果未能解决你的问题,请参考以下文章
我在 SCM/build 工作。我如何告诉非程序员我在做啥?