Chapter 12 move function not working (page 326)

I am having issues with the move function in the player class on chapter 12 in the tile map.

func move(target: CGPoint) {
guard let physicsBody = physicsBody else { return }
let newVelocity = (target - position).normalized()
* PlayerSettings.playerSpeed
physicsBody.velocity = CGVector(point: newVelocity)
}

The error I am getting is that in the newVelocity constant it says that the binary operator “-” cannot be applied to two CGPoint operands. I copy pasted the code to make sure it wasn’t a typo and still got the same error.

@killarviper - In the starter project, you’ll find a folder called SKUtils. This has some methods that make things easier. Are you using the starter project?

In this example, you’re doing a CGPoint1 - CGPoint2 arithmetic. You normally can’t do this, and you’d have to do CGPoint1.x - CGPoint2.x then CGPoint1.y - CGPoint2.y.

In the file CGPoint+Extensions.swift there’s a function that allows you to do both these at once:

/**
 * Subtracts two CGPoint values and returns the result as a new CGPoint.
 */
public func - (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x - right.x, y: left.y - right.y)
}

I only mention that out of interest, but the main point is that you need to have the SKUtils folder in your project to be able to use this function.

Hmm thats odd, I started with just the starter project and I don’t remember deleting any files in the project, maybe it was a bug with xcode because I still have the SKTUtils folder in the project but it has no files inside of it. Thanks for the help!