Reputation: 977
I have a class Branch and I have implemented my custom initializer for it. I got an error stating that I have not implemented the init?(coder aDecoder)
initializer, which I implemented after that. I am still getting an error saying
Fatal error: Use of unimplemented initializer 'init(size:)' for class 'wwdcGame_Sources.Branch'
I tried implementing the initializer but I am just getting more and more errors. And I could not find any other answer to this.
Any help is much appreciated. I have been sitting on this issue for a long time. Thanks in advance.
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))
}
}
I am using this class in turn in another scene. Including this class just in case.
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)
}
}
}
EDIT: This is the contents.swift file where I instantiate the classes
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
Upvotes: 0
Views: 506
Reputation: 2089
Override init(size:)
:
public override init(size: CGSize) {
super.init(size: size)
}
As you have a custom initializer, your Branch
class does not inherit the init(size:)
.
Upvotes: 2