简体   繁体   中英

Can't invoke class with override init method

I'm trying to create a simple game from a tutorial but the code does not seem to be working, so I assume Swift itself has changed since it was written. I'm using XCode 7.3.1 with Swift 2.2.

I'm creating a new class that inherits from SKScene and overrides the original init method:

import SpriteKit

class BallScene: SKScene {
    override init (size: CGSize) {
        super.init(size: size)    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

The GameViewController class fails to create an instance of the BallScene class and reports errors (marked *>): Can't invoke initializer for 'BallScene' with argument list of type '(size: CGSize, () -> () )'

import UIKit
import SpriteKit

    class GameViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

      *>      let scene = BallScene(size: view.bounds.size) {
                // Configure the view.
                let skView = self.view as! SKView
                skView.showsFPS = true
                skView.showsNodeCount = true
                skView.ignoresSiblingOrder = true
      *>         scene.scaleMode = .AspectFill
      *>         skView.presentScene(scene)
            }
        }
//more code
}

Obviously the argument type of the init is being rejected, yet all seems to be ok.

Any ideas?

Many thanks. Kw

Not too sure what you are doing. Why do you have a {} braces after init-ing the BallScene . I try your code, removing the {} works.

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = BallScene(size: view.bounds.size)
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .aspectFill
        skView.presentScene(scene)
    }
//more code
}

If this is not what you wanted, can you explain why you try to have a {} after init-ing BallScene ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM