The included code for Chapter 11, Challenge 2 (changing fill-colors of the Hint arrow) doesn’t seem to make any changes to the appropriate file: HintNode.swift.
I’ve gotten it to work with my own coding, but I’d like to check my solution against the author’s recommendation. Am I looking in the wrong place for the Challenge 2 solution, or did the author accidentally omit it?
It does seem to be incomplete.
My changes
class HintNode: SKSpriteNode, EventListenerNode, InteractiveNode
{
var shape = SKShapeNode()
var colors = [UIColor]()
var colorInt = 0
func didMoveToScene()
{
isUserInteractionEnabled = true
colors = [.red,.orange,.yellow]
…
shape.fillTexture = SKTexture(imageNamed: “wood_tinted”)
shape.alpha = 0.8
shape.fillColor = colors[colorInt]
// shape.fillColor = SKColor.green
…
Added
override func touchesEnded(_ touches: Set, with event: UIEvent?)
{
super.touchesEnded(touches, with: event)
interact()
}
func interact()
{
colorInt = colorInt + 1
if colorInt > 2
{
colorInt = 0
}
shape.fillColor = colors[colorInt]
print("Change fill color to \(colors[colorInt])")
}
A little rough but it seems to work.
Just thought I’d post my solution. Thanks for the first reply to this post. It helped lead me in the right direction.
import SpriteKit
class HintNode: SKSpriteNode, EventListenerNode, InteractiveNode {
var shape = SKShapeNode()
var arrowPath: CGPath = {
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0.5, y: 65.69))
bezierPath.addLine(to: CGPoint(x: 74.99, y: 1.5))
bezierPath.addLine(to: CGPoint(x: 74.99, y: 38.66))
bezierPath.addLine(to: CGPoint(x: 257.5, y: 38.66))
bezierPath.addLine(to: CGPoint(x: 257.5, y: 92.72))
bezierPath.addLine(to: CGPoint(x: 74.99, y: 92.72))
bezierPath.addLine(to: CGPoint(x: 74.99, y: 126.5))
bezierPath.addLine(to: CGPoint(x: 0.5, y: 65.69))
bezierPath.close()
return bezierPath.cgPath
}()
func didMoveToScene() {
isUserInteractionEnabled = true
color = SKColor.clear
shape = SKShapeNode(path: arrowPath)
shape.strokeColor = SKColor.gray
shape.lineWidth = 4
shape.fillColor = SKColor.white
shape.fillTexture = SKTexture(imageNamed: "wood_tinted")
shape.alpha = 0.8
let move = SKAction.moveBy(x: -40, y: 0, duration: 1.0)
let bounce = SKAction.sequence([
move, move.reversed()])
let bounceAction = SKAction.repeat(bounce, count: 100)
shape.run(bounceAction, completion: {
self.removeFromParent()
})
addChild(shape)
}
func interact() {
let color = [SKColor.red, SKColor.yellow, SKColor.orange]
shape.fillColor = color[Int(arc4random_uniform(3))]
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
interact()
}
}