无法转换类型的值" inout CGAffineTransform"预期的论点

时间:2016-07-04 02:05:22

标签: swift sprite-kit

我试图让玩家在路径中改变方向,问题是每次我运行应用程序时都会弹出错误"无法转换类型的值" inout CGAffineTransform& #34;预期的论点......"

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    if gameStarted == false {

        gameStarted = true
        moveClockWise()
        movingClockWise = true


        var myCircle : CGMutablePath! = CGPathCreateMutable()
        let newDx = player.position.x - self.frame.width / 2
        let newDy = player.position.y - self.frame.height / 2
        let newRad = atan2(newDy, newDx)
        let newPath = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: newRad, endAngle: newRad + CGFloat(M_PI * 4), clockwise: true)

        var mirroring = CGAffineTransformMakeScale(1.0, -1.0) // flip horizontal
        var mirrorPath : CGMutablePath! = CGPathCreateMutable()
        let finalPath = withUnsafeMutablePointer(&mirroring)//Here it tells me the error
        {
            CGPathAddPath(mirrorPath, UnsafeMutablePointer($0), newPath.CGPath!)
        }

        let newFollow = SKAction.followPath(mirrorPath, asOffset: false, orientToPath: true, speed: 200)
        player.runAction(SKAction.repeatActionForever(newFollow).reversedAction(),withKey:"followPath")


   }else  {
        player.removeActionForKey("followPath")
    }

       }

func moveClockWise(){

    let dx = player.position.x - self.frame.width / 2
    let dy = player.position.y - self.frame.height / 2

    let rad = atan2(dy, dx)

    path = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

    let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
    player.runAction(SKAction.repeatActionForever(follow).reversedAction(),withKey:"followPath")

}

1 个答案:

答案 0 :(得分:1)

我建议您将代码更改为:

    var mirroring = CGAffineTransformMakeScale(1.0, -1.0) // flip horizontal
    var mirrorPath : CGMutablePath! = CGPathCreateMutable()
    CGPathAddPath(mirrorPath, &mirroring, newPath.CGPath)

    let newFollow = SKAction.followPath(mirrorPath, asOffset: false, orientToPath: true, speed: 200)
    player.runAction(SKAction.repeatActionForever(newFollow).reversedAction(),withKey:"followPath")

来自Apple的文档Interacting with C API's

  

恒定指针

     

当函数声明为采用UnsafePointer参数时,   它可以接受以下任何一种:

     
      
  • nil,作为空指针传递。
  •   
  • 一个UnsafePointer,   UnsafeMutablePointer或AutoreleasingUnsafeMutablePointer   value,如果需要,将转换为UnsafePointer。
  •   
  • 甲   字符串值,如果Type为Int8或UInt8。该字符串将自动   在缓冲区中转换为UTF8,并指向该缓冲区的指针   传递给函数。
  •   
  • 左侧的inout表达式   操作数的类型为Type,它作为指向地址的指针传递   左侧标识符。
  •   
  • 一个[类型]值,作为一个传递   指向数组开头的指针。
  •   

第4个子弹告诉我们您可以发送&mirroring来查找期望UnsafePointer<CGAffineTransform>的参数。