1

Hey i m trying to make a scene for my game as in cod

class GameSCNScene:SCNScene{
let scnView: SCNView!
let _size:CGSize!
var scene:SCNScene!

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
init(currentview view: SCNView) {
    super.init()

    scnView = view
    _size = scnView.bounds.size
    // retrieve the SCNView
    scene = SCNScene()
    scnView.scene = scene
    scnView.allowsCameraControl = true
    scnView.showsStatistics = true
    scnView.backgroundColor = UIColor.blackColor()

}

} but the compiler give this error " Getter for 'scene' with Objective-C selector 'scene' conflicts with initializer 'init()' from superclass 'SCNScene' with the same Objective-C selector" i don,t understand how to remove this error ... please help me i m new to swift

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
Flying
  • 159
  • 12

1 Answers1

0

The error message is telling you that there's already a method named scene defined on the superclass, and your new definition conflicts with it. It's a class method, with Objective-C signature [SCNScene scene], which comes across to Swift as init(). The superclass's scene method conflicts with your var scene:SCNScene!. That's the answer to the question you asked.

However...

It makes no sense to have a scene on a scene. That's what you've defined.

It is very questionable practice to define one view property on an SCNScene. A scene can be displayed in multiple views simultaneously. It can be rendered without a view at all.

SCNScene is not meant to be subclassed. There's a discussion of that here: Trouble subclassing SCNScene. Apple SceneKit engineers are on record here as recommending against it. (I painted myself into a corner over the weekend by trying to subclass SCNNode; don't subclass it either!)

The most common practice seems to be to move the initialization code which you've shown us over to your view controller. That's a fairly simple, reasonable approach for a beginner. That architecture, though, makes for very complicated, bloated view controllers. The best approach is to put your game logic in its own game controller class.

Think of the SCNScene as data. Your game controller contains the logic. When you're ready to add a second level to your game, you write a second SCNScene, and load it, using the same game controller. When you're ready to move your game from iOS to OS X or tvOS, you'll deal with writing a new view controller, but can reuse your scenes and your game controller.

Since you're new to Swift, your first stop should be Daniel Steinberg's book "A Swift Kickstart", available on iBooks.

Community
  • 1
  • 1
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42