0

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

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.