0

I'm using GameplayKit and Swift. In my agent's move component, I'm running agentWillUpdate: but get an error "Cannot invoke initialiser for type 'Float2' with an argument list of type '(CGPoint)'" on the line where position is determined.

MoveComponent: GKAgent2D, GKAgentDelegate {
init(...)
func agentWillUpdate(agent: GKAgent) {

    guard let spriteComponent = entity?.componentForClass(SpriteComponent.self) else {
      return
    }

    position = float2(spriteComponent.node.position)
  } 

The node position is ok (I've tested this with a print). When I CMD-click through 'position' I'm brought to SpriteKit's position property, instead of GKAgent2D's property. When I try to reference property with agent.property, the debugger tells me 'Value of type 'GKAgent' has no member 'position.'

In the next function call, I get the same error as my original one in reverse, "Cannot invoke initialiser for type 'CGPoint' with an argument list of type '(vector_float2)'"

func agentDidUpdate(agent: GKAgent) {
        guard let spriteComponent = entity?.componentForClass(SpriteComponent.self) else {
            return
        }

    spriteComponent.node.position = CGPoint(position)
}

CMD-clicking through the CGPoint(position) brings me to GKAgent2D. It seems the two "positions" are reversed with each other. Any ideas how to correct this?

Shane O'Seasnain
  • 3,534
  • 5
  • 24
  • 31
  • It is a bug I have encounted with XCode, it sometimes gets confused as to what property it is looking for, and will pick what it thinks is best. Did you try doing a total clean build (CMD + SHIFT + ALT + K)? – Knight0fDragon Dec 17 '15 at 13:49
  • Thanks. I tried a clean build but still have the errors – Shane O'Seasnain Dec 17 '15 at 13:52
  • Have a look a this, maybe it will help you http://stackoverflow.com/questions/31343504/how-to-convert-between-vector-float2-and-cgpoint – Knight0fDragon Dec 17 '15 at 13:56

1 Answers1

2

If you don't want to write the conversion code every time you can use these extensions and then you can keep the code you had in your question.

extension CGPoint {
  init(_ point: float2) {
    x = CGFloat(point.x)
    y = CGFloat(point.y)
  }
}

extension float2 {
  init(_ point: CGPoint) {
    self.init(x: Float(point.x), y: Float(point.y))
  }
}
Allanah Fowler
  • 1,251
  • 16
  • 17