Skip to content

Commit

Permalink
Fix crash when image is nil (#10)
Browse files Browse the repository at this point in the history
* Convert `texture` property to optional

* Add `init` func default value for image parameter

* Set `texture` nil when image is nil
  • Loading branch information
mlight3 authored and efremidze committed May 12, 2017
1 parent 776542d commit c04d059
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Sources/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ open class Node: SKShapeNode {
*/
open var image: UIImage? {
didSet {
guard let image = image else { return }
texture = SKTexture(image: image)
if let image = image {
texture = SKTexture(image: image)
} else {
texture = nil
}
}
}

Expand All @@ -75,7 +78,7 @@ open class Node: SKShapeNode {
set { sprite.color = newValue }
}

private(set) var texture: SKTexture!
private(set) var texture: SKTexture? = nil

/**
The selection state of the node.
Expand All @@ -102,7 +105,7 @@ open class Node: SKShapeNode {
- Returns: A new node.
*/
public convenience init(text: String?, image: UIImage?, color: UIColor, radius: CGFloat) {
public convenience init(text: String?, image: UIImage? = nil, color: UIColor, radius: CGFloat) {
self.init()
self.init(circleOfRadius: radius)

Expand Down Expand Up @@ -137,7 +140,9 @@ open class Node: SKShapeNode {
*/
open func selectedAnimation() {
run(.scale(to: 4/3, duration: 0.2))
sprite.run(.setTexture(texture))
if let texture = texture {
sprite.run(.setTexture(texture))
}
}

/**
Expand Down

0 comments on commit c04d059

Please sign in to comment.