Swift Playground - 在课堂上使用未实现的初始化程序'init(size :)'

时间:2018-04-01 12:31:30

标签: ios swift sprite-kit swift-playground initializer

我有一个类分支,我已经为它实现了我的自定义初始化程序。我收到一条错误消息,指出我没有实现init?(coder aDecoder)初始化程序,之后我实现了它。我仍然收到错误说

Fatal error: Use of unimplemented initializer 'init(size:)' for class 'wwdcGame_Sources.Branch'

我尝试实现初始化程序,但我只是遇到越来越多的错误。我找不到任何其他答案。

非常感谢任何帮助。我一直坐在这个问题上很长一段时间。提前谢谢。

Branch.swift

import Foundation
import SpriteKit
import CoreGraphics

public class Branch : SKScene {

    var begin = CGPoint()
    var end = CGPoint()
    var finished: Bool = false

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    public required init(_ begin: CGPoint,_ end: CGPoint,_ finished: Bool){
        self.begin = begin
        self.end = end
        self.finished = finished
        super.init()
    }

    public func show() {
        var points = [CGPoint(x: begin.x, y: begin.y),
                      CGPoint(x: end.x, y: end.y)]
        let line = SKShapeNode(points: &points,
                                          count: points.count)
        line.lineWidth = 5
        addChild(line)
    }

    public func branchRight() -> Branch {

        let angle : Float = (30 * Float(Double.pi) / 180)
        let point = CGPoint(x: end.x - begin.y , y : end.y - begin.y)
        let dir_x = Float(point.x)
        let dir_y = Float(point.y)

        var rotatedPoint : CGPoint = point
        rotatedPoint.x = CGFloat(dir_x * cosf(angle) - dir_y * sinf(angle)) * 0.67
        rotatedPoint.y = CGFloat(dir_y * cosf(angle) + dir_x * sinf(angle)) * 0.67

        let newEnd = CGPoint(x : end.x + rotatedPoint.x, y: end.y + rotatedPoint.y)
        return(Branch(end,newEnd,false))
    }

    public func branchLeft() -> Branch{

        let angle : Float = -(30 * Float(Double.pi) / 180)
        let point = CGPoint(x: end.x - begin.y , y : end.y - begin.y)
        let dir_x = Float(point.x)
        let dir_y = Float(point.y)

        var rotatedPoint : CGPoint = point
        rotatedPoint.x = CGFloat(dir_x * cosf(angle) - dir_y * sinf(angle)) * 0.67
        rotatedPoint.y = CGFloat(dir_y * cosf(angle) + dir_x * sinf(angle)) * 0.67

        let newEnd = CGPoint(x : end.x + rotatedPoint.x, y: end.y + rotatedPoint.y)
        return(Branch(end,newEnd,false))

    }

}

我在另一个场景中轮流使用这个类。包括这个类以防万一。

FractalTreesScene.swift

import SpriteKit
import Foundation
import CoreGraphics

public class FractalTreesScene: SKScene {

    var tree = [Branch]()
    var leaves = [CGPoint]()

    var count = 0;
    let tree_width = 400
    let tree_height = 0

    override public func didMove(to view: SKView) {
        self.scene!.backgroundColor = SKColor.black

        let a = CGPoint(x: tree_width,y: tree_height)
        let b = CGPoint(x: tree_width,y: tree_height + 100)
        let root = Branch(a,b,false)
        tree[0] = root

        for i in stride(from: tree.count, to: 0, by: -1) {
            if (tree[i].finished == false) {
                tree.append(tree[i].branchRight());
                tree.append(tree[i].branchLeft());
            }
            tree[i].finished = true;
            count += 1;

            if (count == 6) {
                for i in 0..<tree.count {
                    if (!tree[i].finished) {
                        let leaf = tree[i].end
                        leaves.append(leaf);
                    }
                }
            }
        }

        for i in 0..<tree.count {
            tree[i].show()
        }

        for i in 0..<leaves.count {
            let leaf = SKShapeNode(circleOfRadius: 3.0)
            leaf.position = leaves[i]
            leaf.fillColor = .red
            addChild(leaf)
        }

    }

}

编辑:这是我实例化类

的contents.swift文件
import PlaygroundSupport
import SpriteKit

let view = SKView(frame: CGRect(x: 0, y: 0, width: 800,height:800))

if let scene = FractalTreesScene(fileNamed: "FractalTreesScene") {
    scene.scaleMode = .aspectFill
    view.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = view

1 个答案:

答案 0 :(得分:2)

覆盖init(size:)

public override init(size: CGSize) {
    super.init(size: size)
}

由于您有自定义初始值设定项,因此Branch类不会继承init(size:)