fatal error: use of unimplemented initializer 'init(size:)' for class

前端 未结 2 1576
被撕碎了的回忆
被撕碎了的回忆 2021-01-11 12:19

I was testing my app on different devices and realized the sprite movements were quite inconsistent (running considerably faster on some devices as compared to others). I fo

2条回答
  •  梦谈多话
    2021-01-11 13:09

    Here is how your code layout should look. As you can see, I designed the scene to be the size of iPhone 6. This means on all other phones, the image will scale (but you will still see everything), but will look perfect on the 6. On the iPad, the image will get chopped off on the top and the bottom by 12.5% each, due to the iPad being a 3:4 and not 9:16

    class GameViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    
        let skView = self.view as? SKView
    
        if skView?.scene == nil  {
            skView?.showsFPS = true
            skView?.showsNodeCount = true
            skView?.showsPhysics = true
            skView?.ignoresSiblingOrder = false
    
            //starting the game with the Poster Scene
            let posterScene = PosterScene(size:CGSize(width:375,height:667))
            posterScene.scaleMode = .aspectFill
            skView?.presentScene(posterScene)
        }
    
    }
    
    override var shouldAutorotate : Bool {
        return true
    }
    
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return UIInterfaceOrientationMask.allButUpsideDown
        } else {
            return UIInterfaceOrientationMask.all
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }
    
    override var prefersStatusBarHidden : Bool {
        return true
    }
    
    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)!
    }
    }
    

    and

    class PosterScene: SKScene {
    
     override init(size:CGSize){
        super.init(size:size)
        self.anchorPoint = CGPoint(x:0.5,y:0.5) //let's put 0,0 at the center of the screen
        let posterImage = SKSpriteNode(imageNamed: "poster")
        posterImage.position = CGPoint.zero
        self.addChild(posterImage)
    
        let sequence = SKAction.sequence([  SKAction.wait(forDuration: 3.0), SKAction.run({ self.changeToMainMenuScene() }) ])
    
        self.run(sequence)
    
    }
    
    func changeToMainMenuScene  ()  {
    
        let mainMenuScene = MainMenuScene()
        self.view!.presentScene(mainMenuScene)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    }
    

提交回复
热议问题