我找不到解決這個問題,也許我沒有實現它的權利。當我運行它時,我只是得到一個空白的白色屏幕。我嘗試了不同的方法,我已經看到這個網站,但沒有一個工作。該錯誤在「fatalError(」init(coder :)尚未實現「)」中突出顯示。我有什麼明顯的缺失?我得到一個錯誤在「fatalError(」初始化(編碼:) :)還沒有實施「)」
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()
}
}
的可能的複製[致命錯誤:初始化(編碼器:)一直沒有執行錯誤,儘管正在實施(https://開頭計算器.com/questions/38966565/fatal-error-initcoder-has-not-been-implemented-errors-though-being-implement) –
我該如何解決它? –